?
Solved

Posted value to set session variable in Php

Posted on 2010-11-28
2
Medium Priority
?
371 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

Will your db performance match your db growth?

In Percona’s white paper “Performance at Scale: Keeping Your Database on Its Toes,” we take a high-level approach to what you need to think about when planning for database scalability.

Question has a verified solution.

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

This article discusses four methods for overlaying images in a container on a web page
Preparing an email is something we should all take special care with – especially when the email is for somebody you may not know very well. The pressures of everyday working life stacked with a hectic office environment can make this a real challen…
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…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses

800 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