Solved

Posted value to set session variable in Php

Posted on 2010-11-28
2
366 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
php date - compare 4 40
nagios 4 php error after installation 6 73
Echo images using file system 2 30
Website Interactive tool that will do calculations 3 26
Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
This article discusses how to create an extensible mechanism for linked drop downs.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

911 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

21 Experts available now in Live!

Get 1:1 Help Now