Should be simple.login question
Moderator: General Moderators
Should be simple.login question
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.
Thanks.
Code: Select all
echo $_SESSION['myusername'];Thanks for the response.
That makes sense but it outputs nothing:
That's strange.
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'];
}
?>- feyd
- Neighborhood Spidermoddy
- Posts: 31559
- Joined: Mon Mar 29, 2004 3:24 pm
- Location: Bothell, Washington, USA
Where are you setting it? Maybe you're storing nothing?
var_dump($_SESSION) may lead to some info to help you debug it.
var_dump($_SESSION) may lead to some info to help you debug it.
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.
I see "session_register(username)" and everything works I don't see why its saying NULL later.
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.
- R4000
- Forum Contributor
- Posts: 168
- Joined: Wed Mar 08, 2006 12:50 pm
- Location: Cambridge, United Kingdom
After registering the seesion, you have to set the value.
Code: Select all
$_SESSION['myusername'] = $THE_USER_NAME;Great! It worked, thanks!
The combined code for anyone else that wants to know:
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");
}- feyd
- Neighborhood Spidermoddy
- Posts: 31559
- Joined: Mon Mar 29, 2004 3:24 pm
- Location: Bothell, Washington, USA
lose the call to session_register().
- R4000
- Forum Contributor
- Posts: 168
- Joined: Wed Mar 08, 2006 12:50 pm
- Location: Cambridge, United Kingdom
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.
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.