tantan6611
asked on
Simple session example
I am really new to php and Mysql
I want to know how to use sessions in order to send username var from page to page in a simple way
I have three files through which I want to make simple example of a session
My target is to echo the username by the main.php file through session username var registration.
what code should I add to these three files in order to do this?
---------------------
userloginform.php
---------------------
<html>
<head>
<title>Users Login Pgae</title>
</head>
<body>
<form action="userlogin.php" method="post">
Username: <input type="username" name="username" />
password: <input type="password" name="password" />
<input type="submit" />
<p> </p>
</form>
</body>
</html>
-----------------
userlogin.php
-----------------
<?php
error_reporting(E_ALL);
?>
<html>
<head>
<title>Login</title>
</head>
<body>
<?php
$con = mysql_connect("localhost", "root","") ;
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("my_db", $con);
$username = mysql_real_escape_string($ _POST["use rname"]);
$password = mysql_real_escape_string($ _POST["pas sword"]);
$result = mysql_query("SELECT * FROM users
WHERE ( username = '$username' ) and ( password = '$password' ) ");
if ($row = mysql_fetch_array($result) )
{
echo "welcome " . $username . " !";
echo "<br>";
echo "<a href='main.php'>Click here to go to the main page</a>";
} else {
echo "Incorrect username or password";
echo "<br>";
@include("userloginform.ph p");
}
?>
</body>
</html>
--------------------
main.php
--------------------
<?php
echo "your username is: " . $username . " ."
?>
I want to know how to use sessions in order to send username var from page to page in a simple way
I have three files through which I want to make simple example of a session
My target is to echo the username by the main.php file through session username var registration.
what code should I add to these three files in order to do this?
---------------------
userloginform.php
---------------------
<html>
<head>
<title>Users Login Pgae</title>
</head>
<body>
<form action="userlogin.php" method="post">
Username: <input type="username" name="username" />
password: <input type="password" name="password" />
<input type="submit" />
<p> </p>
</form>
</body>
</html>
-----------------
userlogin.php
-----------------
<?php
error_reporting(E_ALL);
?>
<html>
<head>
<title>Login</title>
</head>
<body>
<?php
$con = mysql_connect("localhost",
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("my_db", $con);
$username = mysql_real_escape_string($
$password = mysql_real_escape_string($
$result = mysql_query("SELECT * FROM users
WHERE ( username = '$username' ) and ( password = '$password' ) ");
if ($row = mysql_fetch_array($result)
{
echo "welcome " . $username . " !";
echo "<br>";
echo "<a href='main.php'>Click here to go to the main page</a>";
} else {
echo "Incorrect username or password";
echo "<br>";
@include("userloginform.ph
}
?>
</body>
</html>
--------------------
main.php
--------------------
<?php
echo "your username is: " . $username . " ."
?>
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thank you rhickmott and psimation for the fast reply...
However, I am partially aware about the the session concepts you mentioned
but what confuses me is how to apply these commands in my file codes
Can you help me and clarify this through placing the right code inside the codes of the previously included three files of my first post.
Your help is much appreciated.
However, I am partially aware about the the session concepts you mentioned
but what confuses me is how to apply these commands in my file codes
Can you help me and clarify this through placing the right code inside the codes of the previously included three files of my first post.
Your help is much appreciated.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Hi rhickmott
The userlogin.php is giving me :
Warning: Cannot modify header information - headers already sent by (output started at userlogin.php:12) in userlogin.php on line 34
when I enter a correct username and password.
The userlogin.php is giving me :
Warning: Cannot modify header information - headers already sent by (output started at userlogin.php:12) in userlogin.php on line 34
when I enter a correct username and password.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
ok this is fine
but what about if I wanna pass the $username to a fourth page linked with the main.php page?
but what about if I wanna pass the $username to a fourth page linked with the main.php page?
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Gee it's working ... :^)
Now; if I wanna to end the session it would be through , a log out page containing session_destroy(); isn't it?
And how to make a session expires after certain time interval?
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
rhickmott,
What code does the session.php file contain ?
What code does the session.php file contain ?
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
That's pretty good,
But what about setting the session time, e.g. if I wanna set session time for 1 hour?
But what about setting the session time, e.g. if I wanna set session time for 1 hour?
Put in something like at the end
if (isset($_SESSION['time']) && time() - $_SESSION['time'] > 1800) {
// Session Has Expired
$_SESSION = array();
// Expire Session
echo (" <html>
<head>
<title>Login</title>
</head>
<body>
Session Expired <br />
");
@include("userloginform.ph p");
echo (" </body>
</html>
");
exit();
}
else {
$_SESSION['time'] = time();
}
if (isset($_SESSION['time']) && time() - $_SESSION['time'] > 1800) {
// Session Has Expired
$_SESSION = array();
// Expire Session
echo (" <html>
<head>
<title>Login</title>
</head>
<body>
Session Expired <br />
");
@include("userloginform.ph
echo (" </body>
</html>
");
exit();
}
else {
$_SESSION['time'] = time();
}
http://www.tizag.com/phpT/phpsessions.php :)
Sessions are easy once you get started the main pitfalls are
1) Starting the session AFTER something has been output so be very careful there are no whitespaces ( Spaces or blank lines) above your <?php tag. Also if you make includes of other PHP files make sure they also have no whitespace before the <?php
Any whitespace, echos or output will be interpreted as HTML or text and PHP will have to send headers of some description to the browser in order to output that qhitespace so any session_starts it then encounters will make it throw a hissy fit.