Should be simple.login question

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
xterra
Forum Commoner
Posts: 69
Joined: Mon Mar 06, 2006 12:52 pm

Should be simple.login question

Post 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.
User avatar
neophyte
DevNet Resident
Posts: 1537
Joined: Tue Jan 20, 2004 4:58 pm
Location: Minnesota

Post by neophyte »

Code: Select all

echo $_SESSION['myusername'];
xterra
Forum Commoner
Posts: 69
Joined: Mon Mar 06, 2006 12:52 pm

Post 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. &nbsp; <a href = auth.htm>Login</a>"; 
}
else
{

echo $_SESSION['myusername']; 
}

?>
That's strange.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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.
xterra
Forum Commoner
Posts: 69
Joined: Mon Mar 06, 2006 12:52 pm

Post 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.
User avatar
R4000
Forum Contributor
Posts: 168
Joined: Wed Mar 08, 2006 12:50 pm
Location: Cambridge, United Kingdom

Post by R4000 »

After registering the seesion, you have to set the value.

Code: Select all

$_SESSION['myusername'] = $THE_USER_NAME;
xterra
Forum Commoner
Posts: 69
Joined: Mon Mar 06, 2006 12:52 pm

Post 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");
}
User avatar
R4000
Forum Contributor
Posts: 168
Joined: Wed Mar 08, 2006 12:50 pm
Location: Cambridge, United Kingdom

Post 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 :)
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

lose the call to session_register().
User avatar
R4000
Forum Contributor
Posts: 168
Joined: Wed Mar 08, 2006 12:50 pm
Location: Cambridge, United Kingdom

Post 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.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

If you're using superglobals, you're already angling the script to not use session_register() and the other similar functions.
User avatar
R4000
Forum Contributor
Posts: 168
Joined: Wed Mar 08, 2006 12:50 pm
Location: Cambridge, United Kingdom

Post by R4000 »

Yup your right

*looks stupid*

Damn :P
Post Reply