Page 1 of 1
Adding sessions to an existing script?
Posted: Thu Aug 01, 2002 10:52 am
by bedcor
If I wanted to start using sessions with my login script would i have to change my $user = $HTTP_POST_VARS ['user']; to $user = $_Session['user']?
Posted: Thu Aug 01, 2002 11:02 am
by RandomEngy
You'd do something like:
Code: Select all
$_SESSIONї'user'] = $_POSTї'user'];
Then as long as you've got session_start(); at the top of every page, you can access the $_SESSION['user'] variable on a later page.
Posted: Thu Aug 01, 2002 11:03 am
by bedcor
thanks!

Posted: Thu Aug 01, 2002 11:44 am
by bedcor
Can someone tell me if i'm on the right track?
[syntax=php]<?
session_start();
mysql_connect("localhost", "root") or die("Unable to connect to the server.");
mysql_select_db("ijwHotel") or die("Unable to select the database.");
$username = $HTTP_POST_VARS ['username'];
$password = $HTTP_POST_VARS ['password'];
$_SESSION = $_POST[ 'username' ];
$_SESSION = $_POST[ 'password'];
$sql = "Select real_name From users Where username='$username' and password='$password' ";
$result = mysql_query($sql) or die ("Unable to get results.");
$num = mysql_numrows($result) or die ("You're not authorized to be here. If you feel that you have recieved this message in error, please contact the <a href=\"mailto:
cory@ijws.com\">Administrator</a>");
if($num == 1)
{
echo "<br><br><br>";
echo "<h3><center><b>Welcome you are logged in as a GUEST!</b></center></h3><br>";
echo "<h3><center><b>Your guest name is <i><b>$username</b></i></b></center></h3>";
echo "<br>";
echo "<b>This the session: $session</b>";
echo "<center><b>Here is a list of information pieces you could view!</b></center>";
}
?>[/syntax]
Posted: Thu Aug 01, 2002 12:07 pm
by RandomEngy
Try changing it to:
Code: Select all
<?
session_start();
mysql_connect("localhost", "root") or die("Unable to connect to the server.");
mysql_select_db("ijwHotel") or die("Unable to select the database.");
$_SESSIONї'username'] = $_POSTї'username'];
$_SESSIONї'password'] = $_POSTї'password'];
$sql = "SELECT real_name FROM users WHERE username='{$_SESSIONї'username']}' AND password='{$_SESSIONї'password']}'";
$result = mysql_query($sql) or die ("Unable to get results.");
$num = mysql_num_rows($result);
if($num == 0)
die ("You're not authorized to be here. If you feel that you have recieved this message in error, please contact the <a href="mailto:cory@ijws.com">Administrator</a>");
else
{
echo "<br><br><br>\n";
echo "<h3><center><b>Welcome you are logged in as a GUEST!</b></center></h3><br>\n";
echo "<h3><center><b>Your guest name is <i><b>$_SESSIONї'username']</b></i></b></center></h3>\n";
echo "<br>\n";
echo "<center><b>Here is a list of information pieces you could view!</b></center>\n";
}
?>
I
- changed all $username and $password to $_SESSION['username'] and $_SESSION['password']
- fixed how the session variables were set
- capitalized SQL terms in your search query. (This is good practice as it makes it easy to tell which terms are SQL terms and which terms are names in the database and variables)
- changed mysql_numrows to mysql_num_rows
- changed how the denial worked. If the number of matches is 0, it denies access.
- added newline characters at the end of your echoes. It doesn't change the way to page looks but the HTML source is a lot easier on the eyes.
- deleted the line where you try to echo out $session
Remember, $_SESSION is an associative array that stays around throught different pages. Do
on one page to set the session variable, and on another do
to echo it out. (remember session_start(); at the top of every page)
Good luck.
Posted: Thu Aug 01, 2002 12:19 pm
by bedcor
if I was to run the modified code I get this error:
Parse error: parse error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in c:\apache\apache\htdocs\login1.php on line 23
I don't see what's wrong do yu?
Posted: Thu Aug 01, 2002 12:55 pm
by RandomEngy
Change
Code: Select all
echo "<h3><center><b>Your guest name is <i><b>$_SESSIONї'username']</b></i></b></center></h3>\n";
to
Code: Select all
echo "<h3><center><b>Your guest name is <i><b>{$_SESSIONї'username']}</b></i></b></center></h3>\n";
Posted: Thu Aug 01, 2002 1:03 pm
by bedcor
Thanks for the tip it works now!
My first actual completion of using a session (with your help).