session problem

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
batowiise
Forum Commoner
Posts: 28
Joined: Fri Dec 17, 2010 7:11 am

session problem

Post by batowiise »

Hi all,

I want to implement a session based system whereby after a successful login, some fields on a form will be automatically populated with some session records about the user.

This is my code, but its not behaving the way I want ;

<?php
session_start();
$username = $_SESSION['username'];
$firstname = $_SESSION['firstname'];
$surname = $_SESSION['surname'];
require_once("../admin/db.php");

?>

<form action="process.php" method="post" >
<table>
<tr>
<td width="124">Name:</td>
<td colspan="2"><input name=" uname " id=" uname" size="60" value="<?php echo $username; ?>" /></td>
<td colspan="2">&nbsp;</td>
</tr>

<tr>
<td width="124">Name:</td>
<td colspan="2"><input name=" firstname " id=" firstname " size="60" value="<?php echo $firstname; ?>" /></td>
<td colspan="2">&nbsp;</td>
</tr>
<tr>
<td width="124">Name:</td>
<td colspan="2"><input name="surname" id=" surname " size="60" value="<?php echo $surname; ?>" /></td>
<td colspan="2">&nbsp;</td>
</tr>
</table>
</form>

It is only username that gets filed automatically. I however want to fill the firstname and surname too as well.

Your help is much welcome.

Regards,
rcrd.ortiz
Forum Newbie
Posts: 11
Joined: Sun Dec 26, 2010 10:46 am

Re: session problem

Post by rcrd.ortiz »

Hi, I would bet that $firstname and $surname aren't initialized properly. Try vardumping the content of both and if the returned result is (bool) false you should look up how and when you are aetting the session variables.

XD

Rcrd
batowiise
Forum Commoner
Posts: 28
Joined: Fri Dec 17, 2010 7:11 am

Re: session problem

Post by batowiise »

Hi, how do i initialize the $firstname and $surname? apart from these codes

$firstname = $_SESSION['firstname'];
$surname = $_SESSION['surname'];

But why does $username= $_SESSION['username']; works

Please help.
internet-solution
Forum Contributor
Posts: 220
Joined: Thu May 27, 2010 6:27 am
Location: UK

Re: session problem

Post by internet-solution »

may be the bug is in the code where you are setting the sessions variables?
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: session problem

Post by social_experiment »

Code: Select all

<?php
  var_dump($firstname);
  var_dump($surname);
?>
What results do you get if you dump the variable like another reply suggests? Has $_SESSION['firstname'] and $_SESSION['surname'] been set at all?
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
batowiise
Forum Commoner
Posts: 28
Joined: Fri Dec 17, 2010 7:11 am

Re: session problem

Post by batowiise »

var_dump($firstname) did returned a string.

This is my latest code and is in the process.php.

<?php
session_start();

if ($_POST && !empty($_POST['firstname'])) {
// set session variable
$_SESSION['firstname'] = $_POST['firstname'];
}

?>

However, the returned value does appear in all the subsequent logins. What i want to achieve is that when userA logins in, his firstname should be used and when userB also logs in his firstname should also be used.

I

This works fine with the $username. However, i want to achieve the above stated objective.

What next?

Thanks.
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: session problem

Post by social_experiment »

batowiise wrote:However, the returned value does appear in all the subsequent logins. What i want to achieve is that when userA logins in, his firstname should be used and when userB also logs in his firstname should also be used.
I tend to agree with internet-solution, the problem might be where you set the session variables.

Code: Select all

<?php
 $username = $_SESSION['username'];
 $firstname = $_SESSION['firstname']; 
 $surname = $_SESSION['surname'];  
?>
Where do you set the session variables? From your latest post it looks like you equate the values from another form ($_SESSION['firstname'] = $_POST['firstname']). If that's the case, you set the rest like you set $_SESSION['firstname'] and you shouldn't have any problems.
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
batowiise
Forum Commoner
Posts: 28
Joined: Fri Dec 17, 2010 7:11 am

Re: session problem

Post by batowiise »

It worked when i used the following code:

<?php

session_start();
$username = $_SESSION['username'];
//$firstname = $_SESSION['firstname'];

require_once("../admin/db.php");
$result = mysql_query("SELECT * FROM users WHERE username='$username'") or die(mysql_error());
$rs = mysql_query("SELECT * FROM users WHERE username='$username'") or die(mysql_error());
$firstname = mysql_result($result,0,'firstname');
$surname = mysql_result($rs,0,'surname');

?>

Thank you all
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: session problem

Post by social_experiment »

Code: Select all

<?php

session_start();
$username = $_SESSION['username'];
//$firstname = $_SESSION['firstname'];

require_once("../admin/db.php");
$result = mysql_query("SELECT * FROM users WHERE username='$username'") or die(mysql_error());
$rs = mysql_query("SELECT * FROM users WHERE username='$username'") or die(mysql_error());
$firstname = mysql_result($result,0,'firstname');
$surname = mysql_result($rs,0,'surname');

// you should set the session variables here
$_SESSION['firstname'] = $firstname;
$_SESSION['surname'] = $surname;

?>
You set the session variables after they have been declared otherwise they won't contain anything. Hth
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
Post Reply