Page 1 of 1
Should be simple.login question
Posted: Wed Mar 08, 2006 12:05 pm
by xterra
I got the hardest part done in my opinion, the user can now log in. But how can I, in any php page, output the username. In the welcome screen I would like it to say "Welcome" + username. If I use the "session_is_registered(myusername)" it gives me an integer but not the username?
Thanks.
Posted: Wed Mar 08, 2006 12:12 pm
by neophyte
Posted: Wed Mar 08, 2006 12:22 pm
by xterra
Thanks for the response.
That makes sense but it outputs nothing:
Code: Select all
<?
session_start();
if(!session_is_registered(myusername)){
echo "You are not logged in. <a href = auth.htm>Login</a>";
}
else
{
echo $_SESSION['myusername'];
}
?>
That's strange.
Posted: Wed Mar 08, 2006 12:25 pm
by feyd
Where are you setting it? Maybe you're storing nothing?
var_dump($_SESSION) may lead to some info to help you debug it.
Posted: Wed Mar 08, 2006 1:07 pm
by xterra
Ok I used the var code and I see this:
array(1) { ["myusername"]=> NULL }
So some how Im not storing anything.
The login PHP code is sorta basic, I used a tutorial for help. This is just a peice of it, I figured this was automatically storing everything once its in the databse.
Code: Select all
// Define $myusername and $mypassword
$myusername=$_POST['myusername'];
$mypassword=$_POST['mypassword'];
$sql="SELECT * FROM $tbl_name WHERE username='$myusername' and password='$mypassword'";
$result=mysql_query($sql);
// Mysql_num_row is counting table row
$count=mysql_num_rows($result);
// If result matched $myusername and $mypassword, table row must be 1 row
if($count==1){
// Register $myusername and redirect to file "login_success.php"
session_register("myusername");
header("location:login_sucess.php");
}
else {
header("location:login_failure.htm");
}
I see "session_register(username)" and everything works I don't see why its saying NULL later.
Posted: Wed Mar 08, 2006 1:09 pm
by R4000
After registering the seesion, you have to set the value.
Code: Select all
$_SESSION['myusername'] = $THE_USER_NAME;
Posted: Wed Mar 08, 2006 1:14 pm
by xterra
Great! It worked, thanks!
The combined code for anyone else that wants to know:
Code: Select all
// Define $myusername and $mypassword
$myusername=$_POST['myusername'];
$mypassword=$_POST['mypassword'];
$sql="SELECT * FROM $tbl_name WHERE username='$myusername' and password='$mypassword'";
$result=mysql_query($sql);
// Mysql_num_row is counting table row
$count=mysql_num_rows($result);
// If result matched $myusername and $mypassword, table row must be 1 row
if($count==1){
// Register $myusername and redirect to file "login_success.php"
session_register("myusername");
$_SESSION['myusername'] = $myusername;
header("location:login_sucess.php");
}
else {
header("location:login_failure.htm");
}
Posted: Wed Mar 08, 2006 1:15 pm
by R4000
I'm glad it worked for you
I know im new here, but i'll be a great help to some of you

Posted: Wed Mar 08, 2006 1:24 pm
by feyd
lose the call to
session_register().
Posted: Wed Mar 08, 2006 2:39 pm
by R4000
Feyd, i know i'm new, and i'll proberly get blammed for arguing with you.
But i do belive that if you want your code to be as standard compliant and as portable as possable.
You should use session_register();
I admit i never use it, but if this guy is using it, i'd encourage him to keep using it. Rather than stop now, and need it when he moves to an older PHP version or shares his code with a old PHP user.
Posted: Wed Mar 08, 2006 3:43 pm
by feyd
If you're using superglobals, you're already angling the script to not use session_register() and the other similar functions.
Posted: Wed Mar 08, 2006 3:45 pm
by R4000
Yup your right
*looks stupid*
Damn
