Link to home
Start Free TrialLog in
Avatar of tantan6611
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>&nbsp;</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["username"]);
$password = mysql_real_escape_string($_POST["password"]);


$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.php");

}


?>

</body>
</html>
--------------------
main.php
--------------------


<?php

echo "your username is: " . $username . " ."

?>


SOLUTION
Avatar of Robin Hickmott
Robin Hickmott

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Robin Hickmott
Robin Hickmott

More information on

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.
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of tantan6611

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.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ok this is fine
but what about if I wanna pass the $username to a fourth page linked with the main.php page?
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial

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
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
rhickmott,
What code does the session.php file contain ?
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
That's pretty good,
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.php");
                         
                echo      ("      </body>
                                    </html>
                              ");
                       
                   exit();
        }
          else {
              $_SESSION['time'] = time();    
        }