Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Posted value to set session variable in Php

Posted on 2010-11-28
2
Medium Priority
?
373 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 34

Accepted Solution

by:
Beverley Portlock earned 2000 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

Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

Question has a verified solution.

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

If you don't have the right permissions set for your WordPress location in IIS, you won't be able to perform automatic updates. Here's how to fix the problem.
As tax season makes its return, so does the increase in cyber crime and tax refund phishing that comes with it
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
Suggested Courses

610 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