By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,738 Members | 1,719 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,738 IT Pros & Developers. It's quick & easy.

Passig variables to function ?

P: n/a
I made a script for loging into members area and have one problem im using
functions() to declare procedures to do...
And when i want to use some variable that is out of the function it doesent
work.. WHY? and how could i make it work..

Example is bellow... i wont to use $username in functio logedin() but that
doesent seems to work..

CODE: (problem is marked with comment)

<?php
if(!isset($_SESSION['login'])){
if(isset($_POST['username']) && (isset($_POST['password'])))
{
if (empty($username) || empty($password)){
echo "<p><p>";
echo "<script>alert('Niste unjeli korisničko ime ili zaporku')</script>";
echo "<div align='center'><a href='login_link.php'>Pokušajte
ponovo</a></div>";
echo "<p>";
echo "<div align='center'>Ako ste zaboravili zaporku možete je saznati <a
href='zaboravljena_zaporka_link.php'>ovdje</a>";
exit;
}
user_login();
}elseif(isset($_REQUEST['logout']))
{
unset($_SESSION['login']);
session_destroy();
echo "logged Out <a href='$PHP_SELF'>login</a>";
}else{
display_login_form();
}
}else{
logedin();
}

function user_login(){

$database="podsvij_ribepret";
mysql_connect('localhost','podsvij','hrb3hv6gvr');
@mysql_select_db($database) or die( "Trenutno se nije moguće konektirati na
bazu!!");

$query = "SELECT korisnik,sifra FROM users WHERE korisnik='" .
$_POST['username'] . "'";
$result = mysql_query($query);
if(!$result) die('Trenutno se nije moguce konektirati na bazu!!');
$this_user = mysql_fetch_row($result);

if(($_POST['username'] == $this_user[0]) && ($_POST['password'] ==
$this_user[1])){

session_register('login');
$_SESSION['login'] = 1;

logedin();

}else{

display_login_form();
}
}

function display_login_form()
{
echo "<div align='center' class='velika_slova'>Ako niste registrirani
korisnik, možete se registrirati <a
href='registracija_link.php'>ovdje</a>.</div>";

echo "<div align='center'><form method='post' action='$PHP_SELF'>";
echo "Korisničko ime";
echo "<br><input type='text' name='username'>";
echo "<br>Zaporka";
echo "<br><input type='password' name='password'>";
echo "<br><input type='submit' value=' Uđite '>";
echo "</form></div>";

echo "<div align='center'>Ako ste zaboravili zaporku možete je saznati <a
href='zaboravljena_zaporka_link.php'>ovdje</a>";
}
//HERE IS THE PROBBLEM !!!
function logedin()
{
echo "$username"; // DOES NOT WORK!!
echo "<div class='velika_slova'>Dobrodošli na korisničke stranice portala
www.podvodni-svijet.com..</div>";
echo "<br><img src='admin/ikone/prica.gif' width='32' height='32'
align='middle'><a href='unos_prica_korisnici_link.php'>&nbsp;Dodaj ribolovnu
priču</a>";
}
Jul 17 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
In article <c5**********@bagan.srce.hr>,
".: DeVa :." <FA********@FAKEpodvodni-svijet.com> wrote:
Example is bellow... i wont to use $username in functio logedin() but that
doesent seems to work..


Where is $username supposed to get its value from?

If $username is supposed to be the same as $_POST['username'], this will
work:

// function call:
logedin($_POST['username']);

// function definition:
function logedin($username) {

}

JP

--
Sorry, <de*****@cauce.org> is een "spam trap".
E-mail adres is <jpk"at"akamail.com>, waarbij "at" = @.
Jul 17 '05 #2

P: n/a
Nop. i found what seems to be the problem, i had to use global $username...

------
www.podvodni-svijet.com

"Half-brother in blood,
full brother in heart will I be.
Thou shalt lead and I will follow.
May no new grief divide us."

Valar morghulis!
--
Remove all FAKEs when replying to mail !

Jan Pieter Kunst natipkaše:
In article <c5**********@bagan.srce.hr>,
".: DeVa :." <FA********@FAKEpodvodni-svijet.com> wrote:
Example is bellow... i wont to use $username in functio logedin()
but that doesent seems to work..


Where is $username supposed to get its value from?

If $username is supposed to be the same as $_POST['username'], this
will work:

// function call:
logedin($_POST['username']);

// function definition:
function logedin($username) {

}

JP

Jul 17 '05 #3

P: n/a
In article <c5**********@bagan.srce.hr>,
".: DeVa :." <FA********@FAKEpodvodni-svijet.com> wrote:
Nop. i found what seems to be the problem, i had to use global $username...


That's a possible way to do it. But I find that using function
parameters instead of globals is more readable and leads to code that is
easier to maintain.

JP

--
Sorry, <de*****@cauce.org> is een "spam trap".
E-mail adres is <jpk"at"akamail.com>, waarbij "at" = @.
Jul 17 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.