0

i'm having some issues with PHP sessions. I'm pretty new to PHP so my apologies if i'm being completely stupid.

I have a login.php file that once the user name and password has been checked etc it has the following code:

if($pass === $row2['PSWD']){
    session_start();
    $_SESSION['test']="hello";
    mysql_close($con);
    header("Location: page.php");
}else{
    die('Wrong password');
}

then on page.php I have the following at the very top, about the tag:

<?php
session_start();
session_register(); //just in case...(should not be needed)
echo "Result:".$_SESSION['test'];
?>

And all I get at the top of the page is "Result:"

Any ideas? As from everything i've been reading it should be as simple as this?

Thanks in advance!

EDIT:

My error logs are showing:

Warning: session_start() [function.session-start]: open(/var/php_sessions/sess_b91f8653bcee6ef7c1e13ae8844f00da, O_RDWR) failed: No such file or directory (2) in /hermes/bosweb/web261/b2617/ipg.craigfisk/webtest/login.php on line 28

Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at /hermes/bosweb/web261/b2617/ipg.craigfisk/webtest/login.php:28) in /hermes/bosweb/web261/b2617/ipg.craigfisk/webtest/login.php on line 28

Warning: Cannot modify header information - headers already sent by (output started at /hermes/bosweb/web261/b2617/ipg.craigfisk/webtest/login.php:28) in /hermes/bosweb/web261/b2617/ipg.craigfisk/webtest/login.php on line 31

Warning: Unknown: open(/var/php_sessions/sess_b91f8653bcee6ef7c1e13ae8844f00da, O_RDWR) failed: No such file or directory (2) in Unknown on line 0

Warning: Unknown: Failed to write session data (files). Please verify that the current setting of session.save_path is correct (/var/php_sessions) in Unknown on line 0

  • Are you sure you have no output before session_start at both pages? – Green Black Feb 12 '13 at 16:55
  • possible duplicate: http://stackoverflow.com/questions/3023353/session-lost-after-page-redirect-in-php?rq=1 – Nick Fury Feb 12 '13 at 16:56
  • Try calling `session_write_close()` before the header() redirect code. Also, add exit() after the header() redirect to be safe. – Anton Feb 12 '13 at 16:57
  • @John What do you mean by no output exactly? – Craig Fisk Feb 12 '13 at 16:59
  • John is meaning that you are writing something (even a space) before session_start. – idmean Feb 12 '13 at 17:00
  • That you do not echo anything, or do not write anything to the browser. so: ` – Green Black Feb 12 '13 at 17:00
  • 1
    @anton: write_close is not necessary. unless the script terminates with a fatal error, php will automatically close/save the session during cleanup. – Marc B Feb 12 '13 at 17:25
  • @craig: turn on error display. you've probably got a bunch of warnings/errors suppressed and are working in the dark. NEVER have errors/warnings disabled while developing or debugging. http://www.php.net/manual/en/errorfunc.configuration.php#ini.display-errors http://www.php.net/manual/en/errorfunc.configuration.php#ini.error-reporting – Marc B Feb 12 '13 at 17:26
  • @anton: that's why _SESSION exists. it's an array (plus being a superglobal) like any other, but php will save its contents for you if you've session_start()'d. That's the only "special" thing about it. – Marc B Feb 12 '13 at 17:30
  • Thank's for all the comments guys, @MarcB i've now got my error logs on and have updated my question – Craig Fisk Feb 13 '13 at 09:12

2 Answers2

1

You need to put exit(); after your header redirection, otherwise you have just loaded two pages of content into 1 page.

source: https://stackoverflow.com/a/3023479/710827

Community
  • 1
  • 1
Nick Fury
  • 1,313
  • 3
  • 13
  • 23
0

after header location give exit();