Forum     

Go Back   Digit Technology Discussion Forum > Software > Programming
Register FAQ Calendar Mark Forums Read

Programming The destination for developers - C, C++, Java, Python and the lot


Closed Thread
 
LinkBack Thread Tools Display Modes
Old 05-03-2009, 03:28 PM   #1 (permalink)
Excessive happiness
 
furious_gamer's Avatar
 
Join Date: Jun 2008
Location: Bangalore
Posts: 2,975
Default Session Management in PHP


Guys, i was completely running out of idea when it comes to session tracking. I previously do the session management in JSP/Servlet using the database to store the user's session related details,temporarily,say until he logs out.

Now after i moved to PHP, i was so glad to see that the default session handling offered by PHP is so good to consider. But the problem is how to use the default session handling efficiently.
Consider a scenario i come up with :

After the user presses logout, i just unregister the session variable and destroy the session. But when i try to click the back button of the browser, it say "The Page you are trying to view contains POSTDATA that has expired from cache. If you resend the data, any action the form carried out(such as a search or online purchase) will be repeated. To resend the data, click OK. Otherwise , click Cancel".

After i press OK, it then go back to the previous page, which is only viewable to authorized users.

In my program i have three pages . ( A Simple session management program using PHP)
1. login.php
2.welcome.php
3.logout.php

login.php doesnt contain any php code. It simply has two textfields for username and password and a Submit button.

welcome.php

PHP Code:
<?
session_start
();
$username $HTTP_POST_VARS["username"];
$password $HTTP_POST_VARS["password"];
if(
$username=="somedata" && $password=="someotherdata")
{
echo 
"Authorized user";
session_register("username");
echo 
"<a href="logout.php'>Log Out</a>";
}
else
{
echo "Un-authorized user";
echo "<a href="login.php">Go Back</a>";
}
?>
logout.php

PHP Code:
<?
session_start
();
if(
session_is_registered("username"))
{
session_unregister("username");
session_destroy();
}
else
{
echo 
"Unknown call to this page.";
}
?>
Please let me know whats wrong with my code. I dont want to use database to track the users session. So please try to help me with the default session tracking offered by PHP.
__________________
My First Android phone : Samsung Galaxy SL i9003 - Rooted & Gingerbread XXKPQ
Updated : superteekz_V2 ROM for XXKPQ.

PS Request
furious_gamer is online now  
Advertisements. Register and be a member of the community to get rid of them.
Advertisement

Old 06-03-2009, 01:05 PM   #2 (permalink)
Canon 1000D
 
astroutkarsh's Avatar
 
Join Date: Apr 2007
Location: Pune
Posts: 80
Lightbulb Re: Session Management in PHP

Can you try with No Cache in meta tag on that page? so that data will not be in browser cache.
__________________
Canon 1000D + 18-55 + 50mm f/1.8 + 55-250 IS :-)
astroutkarsh is offline  
Old 06-03-2009, 01:28 PM   #3 (permalink)
spice it up
 
kapsicum's Avatar
 
Join Date: Apr 2004
Location: mumbai
Posts: 106
Default Re: Session Management in PHP

firstly i didnt get your problem but the following is as per what i understood ....

if a user clicks browsers back button after he has logged out ,
and if you dont want the action to be repeated you can validate the session variables before performing any actions like search or Online purchase.
if a user has logged out & on clicking of back button the actions wont be performed since the users session has been unregistered.

check the following codes :

welcome.php :

PHP Code:
<?php // always make a habit of using <?php instead of <? for starting any php code
session_start(); 

// can also use GET or POST depending on ur Login Form Method, REQUEST can be helpful in both the cases,
// never use GET as Form method for sensitive data like password 
$username $_REQUEST["username"];
$password $_REQUEST["password"]; 

if(
$username == "somedata" && $password == "someotherdata")
{
    echo 
"Authorized user";

    
// the use of session_register() is depreciated since PHP 4.1.0
    // its best to use $_SESSION['variable_name'] 
    
$_SESSION["username"] = $username;

    echo 
'<a href="logout.php?user='.$username.'" >Log Out</a>'// take care with using single quotes ( ' ) & double quotes ( " )
}
else
{
    echo 
"Un-authorized user";
    echo 
"<a href="login.php">Go Back</a>";
}
?>

logout.php :

PHP Code:
<?php
session_start
();

$username $_REQUEST["user"]; // can use GET too since the variable is passed in URL

//its better to use isset() instead of session_is_registered() to check if a session is set or not
// also its good to check the value of the particular session variable
if( isset($_SESSION["username"]) && $_SESSION["username"] == $username 
{
    
//again use unset() instead of session_unregister()
    
unset($_SESSION["username"]);
    
session_destroy();
}
else
{
    echo 
"Unknown call to this page.";
}
?>
Note : If you are using $_SESSION (or $HTTP_SESSION_VARS), do not use session_register(), session_is_registered() and session_unregister().
kapsicum is offline  
Old 06-03-2009, 02:40 PM   #4 (permalink)
Excessive happiness
 
furious_gamer's Avatar
 
Join Date: Jun 2008
Location: Bangalore
Posts: 2,975
Default Re: Session Management in PHP

Kapsicum will try it now...
__________________
My First Android phone : Samsung Galaxy SL i9003 - Rooted & Gingerbread XXKPQ
Updated : superteekz_V2 ROM for XXKPQ.

PS Request
furious_gamer is online now  
Closed Thread

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Help:Cannot start session[fedora7] VINSTAR Open Source 13 11-02-2008 07:55 PM
php session save error boosters QnA (read only) 5 24-02-2007 02:06 AM
How to Close session of open session CD vikasg03 QnA (read only) 1 21-09-2006 12:03 PM
MTNL-session history help. blueshift QnA (read only) 1 18-08-2006 10:42 AM
session expired???? what is this??? taken Software Q&A 4 05-05-2005 09:05 AM

 
Latest Threads
- by Sujeet
- by gforz
- by soumya
- by icebags

Advertisement




All times are GMT +5.5. The time now is 03:08 PM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2012, vBulletin Solutions, Inc.

Search Engine Optimization by vBSEO 3.3.2