Solved

Posted value to set session variable in Php

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

Forrester Webinar: xMatters Delivers 261% ROI

Guest speaker Dean Davison, Forrester Principal Consultant, explains how a Fortune 500 communication company using xMatters found these results: Achieved a 261% ROI, Experienced $753,280 in net present value benefits over 3 years and Reduced MTTR by 91% for tier 1 incidents.

Question has a verified solution.

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

If you are a web developer, you would be aware of the <iframe> tag in HTML. The <iframe> stands for inline frame and is used to embed another document within the current HTML document. The embedded document could be even another website.
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
The viewer will learn how to dynamically set the form action using jQuery.
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…

730 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