Page 1 of 1

session problem

Posted: Thu Dec 30, 2010 11:39 am
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,

Re: session problem

Posted: Thu Dec 30, 2010 12:03 pm
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

Re: session problem

Posted: Thu Dec 30, 2010 12:14 pm
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.

Re: session problem

Posted: Thu Dec 30, 2010 12:48 pm
by internet-solution
may be the bug is in the code where you are setting the sessions variables?

Re: session problem

Posted: Thu Dec 30, 2010 5:58 pm
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?

Re: session problem

Posted: Fri Dec 31, 2010 3:10 am
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.

Re: session problem

Posted: Fri Dec 31, 2010 8:47 am
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.

Re: session problem

Posted: Fri Dec 31, 2010 9:10 am
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

Re: session problem

Posted: Sat Jan 01, 2011 5:05 am
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