0

I am trying to set a session variable but it's not working. Here is what I am doing in Code. Please suggest what's wrong:

Login-Validator.php

<?php
    session_start();
    $userName = "test";
    $_SESSION['iUsername'] = $userName;
    header("Location: http://www.XXXXXXXXXXXX.com/LoginSuccess.php");
?>

LoginSuccess.php

<?php
    session_start();
    $User = $_SESSION['iUsername'];
    echo $User;
?>
Jimmy Sawczuk
  • 13,488
  • 7
  • 46
  • 60
nik2702
  • 93
  • 1
  • 2
  • 8
  • 1
    This code seems okay, but your sessions could be failing to persist for a variety of reasons. Please provide some more information, like your deployment environment, your session handler, and what browsers you're using. – Jimmy Sawczuk Oct 02 '12 at 13:58
  • 5
    Are these files on the same server? The reason I ask is because of the 'http://www.XXXXXXXXXXXX.com/' in the header. Also have you tried adding print_r($_SESSION); in both files under the session_start() to see what is in the session? – Chris Oct 02 '12 at 13:59
  • 1
    Are there any hidden errors, such as do you have a space perhaps at the top of the file and its saying the headers cant be sent? – BugFinder Oct 02 '12 at 14:00
  • The code works ok when files are on the same server, btw. – raina77ow Oct 02 '12 at 14:00
  • 1
    possible duplicate of [Cross domain PHP Sessions](http://stackoverflow.com/questions/1339984/cross-domain-php-sessions) – mario Oct 02 '12 at 14:02
  • 1
    i am sure these files are not on same server. – Yogesh Suthar Oct 02 '12 at 14:02
  • Maybe try in each file: error_reporting(E_ALL); - may show up some hidden errors maybe, though I am not sure on this - just stealing an idea that BugFinder suggested.. – Chris Oct 02 '12 at 14:02
  • possible duplicate of [Headers already sent](http://stackoverflow.com/questions/8028957/headers-already-sent-by-php) in case it's the same domain and *someone* didn't look for notices yet. – mario Oct 02 '12 at 14:03
  • 1
    check your `server configuration` first , it is causing problem if files are on same server. – Yogesh Suthar Oct 02 '12 at 14:06

1 Answers1

1

Try this (put a 'exit' after the redirect)

session_start();
$_SESSION['session'] = 'this is a session';
header('location: apage.php');
exit;

read more at @ PHP: session isn't saving before header redirect

If this doesnt work..comment out the redirect and open each page in a different browser tab. Then open Login-Validator.php and then open LoginSuccess.php and check if the session was set. I think it cause by the cookie not setting before the redirect.

Also is Login-Validator.php and LoginSuccess.php on the same domain?

header("Location: /LoginSuccess.php");
Community
  • 1
  • 1
MagePal Extensions
  • 17,646
  • 2
  • 47
  • 62