Solved

Posted value to set session variable in Php

Posted on 2010-11-28
2
364 Views
Last Modified: 2012-05-10
I'm working on a Windows server with IIS webserver that supports ASP and PHP. I simply want to set a session variable according to the value in a posted field. If I have a form in a php file as in the code below where it posts to itself and tries to set the session variable, when it redirects to file test2.php the session variable is not set.

If instead I put the checking of the posted value and the setting of the session variable in file test2.php then the session value does persist, eg if there's a link to test3.php then the session value is still set in test3.php.

Is this normal that the session does not survive a redirect using: header('Location:test2.php');

<?Php

session_start();

if (isset($_POST['field1']) && $_POST['field1'] != '') {

	// some other code here checks the database

        $_SESSION['user'] = 'logged-in';

	header('Location:test2.php');

}

?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

 <title>TEST</title>

</head>

<body style="background-color: #ffffff">



<form name="login" action="test.php" method="post">

  Enter any text to test session handling&nbsp;<input type="text" name="field1" id="field1" />&nbsp;<input type="submit" name="submit" id="submit" value="Sign In" />

</form>



</body>

</html>

Open in new window

0
Comment
Question by:ncw
2 Comments
 
LVL 34

Accepted Solution

by:
Beverley Portlock earned 500 total points
ID: 34225070
Are you using session_start() in test2.php?

Also, after the header statement, place an exit to force the redirection to occur without any further processing. Executing header("Location......"); does NOT make the redirection occur - the script keeps executing until it halts.

if (isset($_POST['field1']) && $_POST['field1'] != '') {
      // some other code here checks the database
        $_SESSION['user'] = 'logged-in';
      header('Location:test2.php');

        exit;   // <---- add this
}
0
 
LVL 1

Author Closing Comment

by:ncw
ID: 34225262
Spot on! It was the exit(); that was missing and has now fixed the issue. Thanks very much!
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Running classic asp applications under Windows Server 2008 R2 (x64) and IIS 7 is not as easy as one may think. It took me a while to figure it out while getting error 8002801d a few times. After you install the OS you will need to install the fol…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now