session problem
Moderator: General Moderators
session problem
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"> </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"> </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"> </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,
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"> </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"> </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"> </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
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
XD
Rcrd
Re: session problem
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.
$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
may be the bug is in the code where you are setting the sessions variables?
- social_experiment
- DevNet Master
- Posts: 2793
- Joined: Sun Feb 15, 2009 11:08 am
- Location: .za
Re: session problem
Code: Select all
<?php
var_dump($firstname);
var_dump($surname);
?>“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
Re: session problem
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.
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.
- social_experiment
- DevNet Master
- Posts: 2793
- Joined: Sun Feb 15, 2009 11:08 am
- Location: .za
Re: session problem
I tend to agree with internet-solution, the problem might be where you set the session variables.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.
Code: Select all
<?php
$username = $_SESSION['username'];
$firstname = $_SESSION['firstname'];
$surname = $_SESSION['surname'];
?>“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
Re: session problem
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
<?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
- social_experiment
- DevNet Master
- Posts: 2793
- Joined: Sun Feb 15, 2009 11:08 am
- Location: .za
Re: session problem
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;
?>
“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