SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Zealot metho's Avatar
    Join Date
    Feb 2005
    Posts
    132
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    sessions not set after header redirect

    Any help would be appreciated in solving this php session problem. Basically I need to set sessions successfully before a header redirect (and ya... it hand tweaked DWMX2004 code)

    After redirected page loads - sessions are not set.

    Fixes attempted but failed:

    1. $_XXX to $HTTP_XXX_VARS
    2. Concatenating ($GLOBALS['PHPSESSID'] ? '?SID='.$GLOBALS['PHPSESSID'] : ''))) to the end of redirect URL.

    n.b redirect uses absolute, not relative

    3. ob_start(); at start & ob_end_flush(); at end of code
    4. Commenting out blank lines in code.


    Code:
    <?php
    ob_start();
    session_start();
    header("Cache-control: private"); 
    require_once...//db connect file
    include..// site config file
    //
    $step = $_POST['step'];
    $email = $_POST['email'];
    $password = $_POST['password'];
    $email = $_POST['email'];
    if($step = '2'){
    if(isset($_SESSION['email'])){
    //
    $_SESSION['email'] = false;
    $_SESSION['password'] = false;
    $_SESSION['access'] = false;
    //
    	}
    }
    ?>
    <?php
    // *** Redirect if email addy exists
    ..snip..// this part of the script works fine
    //
    // Insert if email addy is unique
    function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
    {
      $theValue = (!get_magic_quotes_gpc()) ? addslashes($theValue) : $theValue;
    
      switch ($theType) {
        case "text":
          $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
          break;    
        case "long":
        case "int":
          $theValue = ($theValue != "") ? intval($theValue) : "NULL";
          break;
        case "double":
          $theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL";
          break;
        case "date":
    	  $theValue = ($theValue != "") ? "'" . date("Y-m-d",strtotime($theValue)) . "'" : "NULL";
          break;
    	case "time":
    	  $theValue = ($theValue != "") ? "'" . date("H:i:s",strtotime($theValue)) . "'" : "NULL";
          break;
        case "datetime":
    	  $theValue = ($theValue != "") ? "'" . date("Y-m-d H:i:s",strtotime($theValue)) . "'" : "NULL";
          break;
        case "defined":
          $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
          break;
      }
      return $theValue;
    }
    //
    $editFormAction = $_SERVER['PHP_SELF'];
    if (isset($_SERVER['QUERY_STRING'])) {
      $editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
    }
    // Set sessions for next pages
    if (isset($_POST["MM_insert"])) {
    //
    	$email = $_POST['email'];
    	$password = $_POST['password'];
    	$email = $_POST['email'];
      	$_SESSION['access'] = 0;
    	$_SESSION['email'] = $email;
    	$_SESSION['password'] = $password;
    	session_write_close();
    //
    }
    if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "register")) {
    //	
    	$insertSQL = //...snip... insert works fine
    //
      	mysql_select_db($database_conn_opFair, $conn_opFair);
      	$Result1 = mysql_query($insertSQL, $conn_opFair) or die(mysql_error());
    	$insertGoTo = "test_register_sessions.php";
    	header("Location: ".$root.$insertGoTo);
    	exit();
    }
    ?>

  2. #2
    SitePoint Guru silver trophy Luke Redpath's Avatar
    Join Date
    Mar 2003
    Location
    London
    Posts
    794
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    This isn't really an advanced PHP topic, this should really be in the main PHP forum.

    Because session data is physically stored on the server, any session variables changed/created in your script are not physically saved until the script has finished executing. When you call the header() function, you are interupting the script execution and your session variables do not get saved. This is why you are having this problem.

    To solve this problem, you have to explicitly tell PHP to write the session to disk. You can do this by doing the following:

    PHP Code:
    // make some changes to your session
    $_SESSION['somevar'] = 'some value';
    $_SESSION['someothersessionvar'] = 'someothervalue';

    // save the session (explicitly)
    session_write_close();

    // now redirect
    header('Location: someurl.php'); // etc... 

  3. #3
    SitePoint Zealot metho's Avatar
    Join Date
    Feb 2005
    Posts
    132
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    My apologies /me notes to check which forum he's in b4 posting next time. Unfortunately your solution does not work. I already tried session_write_close(); in a separate function after the insert and before the header = no dice.

    Sessions are working fine on the server for non-header redirects... I'm currently trying to nut out a way of doing it with output control functions.

  4. #4
    SitePoint Guru silver trophy Luke Redpath's Avatar
    Join Date
    Mar 2003
    Location
    London
    Posts
    794
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Can you post your code as session_write_close() should definately fix the problem.

  5. #5
    SitePoint Zealot metho's Avatar
    Join Date
    Feb 2005
    Posts
    132
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    turned out to be the dev server config (no cookies for this purpose). live server has more cookie power and works there.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •