Page 1 of 1

Sessions - How to move between pages

Posted: Thu Jun 01, 2006 12:54 am
by technofreak
Hello,

I am developing a simple user details management app. where I have a login screen. I have designed it using mysql. But if i use the session_start() in the login page and use session_destroy in the logout page, which can be accessed thru the page the user gets into after successfully loging in, i get an error saying " Warning: session_destroy() [function.session-destroy]: Trying to destroy uninitialized session ". :(

How will I initialize a session. How will i maintain the session between login and logout, when I have some 2 or 3 pages inbetween the login and logout pages ?

--------------login page------------------

Code: Select all

<?php session_start();?>

<html>
<head>
<title> User Information Management </title>
</head >

<body>

<?php
if ($_POST['_submit_check']) {      // check whether uname and passwd are entered, if else show the form
     process_form();
} else {
      show_form();
}
?>

</body>
</html>


<?php function show_form() { // Display the login form ?>


<div align="center"><br /><form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>" >   <!-- Login Form-->
<table><tr>
<td>User Name: </td>
<td><input type="text" name="user_name" maxlength="20" /></td>
</tr><tr>
<td>Password: </td>
<td><input type="password" name="pass_word" maxlength="20" /></td>
</tr><tr>
<td align="right" colspan="2" ><input type="submit" name="login_button" value="Login" style="background-color:white; " /></td>
<td><input type="hidden" name="_submit_check" value="1"/> </td>
</tr>
</table>
</form >
</div>

<?php } ?>

function process_form() { //Process the form - check for username and password

$link = mysql_connect('localhost', 'root', 'root');
if ( ! $link ) { die("Can't Connect: " .mysql_error()); }

$uname = $_POST['user_name'];
$pword = $_POST['pass_word'];

mysql_select_db('hdp_um');
$query = "SELECT * FROM login_table WHERE u_name = '$uname' AND p_word = '$pword'";
$q = mysql_query($query);


if ( ! mysql_num_rows($q)) { //if there is any row returned from query, proceed

           print "Login Failed - Wrong Username or Password";
           exit(); }
           
$_SESSION['user_name'] = $uname;

 selection_screen(); }  ?>
---------------------------------logout page---------------------------

Code: Select all

<?php
session_destroy();
?>

<html>
<head>
<title> Logged Out !</title>
<link rel="stylesheet" type="text/css" href="trial_style.css" />
</head>
<body>
<? //echo $_SESSION['user_name']; ?>
<div><h3> User Logged Off From Current Session!</h3></div>
<br />
<div><p>Do You Wish to <a href="dblogin.php"> Login Back ? </a></p></div>

</body>
</html>
---------------------------------------------------------------

Selection screen is the link to next page, where i have placed the 'logout' page link, either as <a href... or as a submit button.

Can anybody point me to good tutor/web site on sessions in PHP ? :)

Posted: Thu Jun 01, 2006 1:30 am
by andym01480

Code: Select all

session_start();
needs to be at the top of every page that uses or will destroy it!

Try http://www.zend.com/php/beginners/index.php. Best turtorial I've found

Oh and edit your post and put php tags around the code, otherwise the moderators will do it for you!

Posted: Thu Jun 01, 2006 1:41 am
by technofreak
andym01480 wrote:

Code: Select all

session_start();
needs to be at the top of every page that uses or will destroy it!

Try http://www.zend.com/php/beginners/index.php. Best turtorial I've found

Oh and edit your post and put php tags around the code, otherwise the moderators will do it for you!

Hey thanks a lot andy.... both for the solution and for the info abt

Code: Select all

tagging. Am new to this forum, thats y i din't know

Posted: Thu Jun 01, 2006 3:26 am
by technofreak
I tried using "session_start();" at the top of the page linked by me.

I declared a session variable in the main_page.php

$_SESSION['user_name'] = $_POST['user_name'];

When I use the same in my next page, which is linked through --> <a href="page2.php">Next Page</a>
But now, when i tried --> echo $_SESSION['user_name'] ,in page2.php it doesnot print the user variable which is declared as session variable in main_page.php

Can some1 help me to access $_POST['user_name'] which is also $_SESSION['user_name'] in my page2.php ?

Posted: Thu Jun 01, 2006 8:58 am
by ok
Maybe you have a problem with cookies... try to use SID, i.e:

Code: Select all

$html = '<a href="page2.php?'.SID.'">...';

Posted: Fri Jun 02, 2006 7:18 am
by technofreak
I have another doubt. If i initialize the session varibale say $_SESSION['name'] within a function in include file, which is called in the main page. The session varible works well in all other functions within the include file. If i try to use it in the next page, which is accessed by a submit button in oen of the functions within the include file, it doesn't work i.e. am not able to use the session variable. For better understanding see the following code..

main.php

Code: Select all

<?php session_start(); include("includepage.php"); ?>

if ($_POST['submit_check']) {
      do_the_thing();
} else {
<form method="POST" action="<? php echo $_SERVER['PHP_SELF']; ?>">
echo input_box();
echo submit_button();
</form>
<? } ?>
includepage.php

Code: Select all

<php function input_box() { ?>
<input type="text" name="user" />
<? } ?>

<php function submit_button() { ?>

<input type="submit" name="submit" value="ok" />
<input type="hidden" name="submit_check" value="1" />
<? } ?>

<?php function do_the_thing() { 
$_SESSION['user'] = $_POST['user'];
?>
<form method="POST" action="page2.php">
<input type="submit" name="agree" value="agree" />
</form>
<?php } ?>




page2.php

Code: Select all

<?php session_start(); 
echo S_SESSION['user'];
?>
In this code, when i enter the 'user' text box and press 'submit', the 'user' value is initialized as session variable by the do_the_thing() function. Then on pressing the 'agree' button, the page2.php is activated and the $_SESSION['user'] needs to be echoed. In my case such a thing is not hapenning. Am i wrong somewhere ? I have included session_start() are both the pages, main.php and page2.php, as adviced in the earlier posts.