-1

** SOLVED ** - Server Problem

As mentioned above, the session ids are the same on both pages but I can't access to the session variables. I need Session for a bigger project I created this to test it. I don't have any Idea why since the ids are the same.

First page:

<?php session_start();
echo "session started"
?>
<!DOCTYPE html>
<html>
<body>

<?php
// Set session variables
$_SESSION["favcolor"] = "green";
$_SESSION["favanimal"] = "cat";
echo "Session variables are set.";
echo "<br>";
print_r(session_id());
echo "<br>";
echo "<a href='use.php'>Display</a>"
?>

</body>
</html>

Second Page:

<?php session_start();
?>
<!DOCTYPE html>
<html>
<body>

<?php
// Echo session variables that were set on previous page
echo "Favorite color is " . $_SESSION["favcolor"] . ".<br>";
echo "Favorite animal is " . $_SESSION["favanimal"] . ".";
echo "<br>";
print_r(session_id());
?>

</body>
</html>
dben
  • 484
  • 1
  • 6
  • 21
  • What output do you get? – Andy Feb 03 '17 at 19:39
  • Nothing at all like if I declare the variables as an empty string. – dben Feb 03 '17 at 19:41
  • 3
    I see the line `echo "Display"` does not end with a semi colon. But that might not be the issue why session not set. – Rajesh Feb 03 '17 at 19:43
  • Tested, Still not working. – dben Feb 03 '17 at 19:45
  • 2
    @Rajesh You don't need a `;` on the last line when you close off with `?>`. If you write ` ` it is still valid. http://stackoverflow.com/questions/2038745/do-i-need-a-trailing-semicolon-here – Rasclatt Feb 03 '17 at 19:48
  • what do you get as the output if you execute `var_dump(is_callable('session_start'));`? – Rajesh Feb 03 '17 at 19:53
  • 1
    what does http://php.net/manual/en/function.error-reporting.php pop out? – Funk Forty Niner Feb 03 '17 at 19:56
  • 1
    Another possible cause for sessions not working as expected is that you have an accidental whitespace or other output before your first 'session_start() . Please enable error_reporting. – steven Feb 03 '17 at 19:57
  • and how are you accessing this? `http://localhost` or hosted, or as `file:///`? few questions to be answered here in order to get to the bottom of this. When you look at your html source, what do you see, code? – Funk Forty Niner Feb 03 '17 at 19:59
  • *Tic ♫ toc ♫ Tic.... Toc ♫* - time is ticking..... so; what's the verdict? I don't have an app for you to install in order to inform me/us as to what the progress is on this question. I feel like I'm watching paint dry. Matter 'o fact, I think it would be more fun. Edit: Ok, I'm outta here; *bye* – Funk Forty Niner Feb 03 '17 at 20:03
  • Sorry guys I travelled without Internet access – dben Feb 04 '17 at 18:35
  • @Rajesh bool(true) is the output. – dben Feb 04 '17 at 18:36
  • @Fred-ii- It's hosted on the Cardiff university web server. – dben Feb 04 '17 at 18:36
  • @steven I checked that before there is nothing before the session start command. – dben Feb 04 '17 at 18:38
  • @Fred-ii- The output is the following code , nothing strange. Favorite color is .
    Favorite animal is .
    bool(true)
    oqqonnvpqctaccgt241a39dp50
    – dben Feb 04 '17 at 18:39
  • I tested on the project server which is only accessible with Cardiff uni login. Now I uploaded it to the public server which can be accessed by anyone and it's working. Probably just some kind of server problem – dben Feb 04 '17 at 18:42

1 Answers1

0

For those with the same problem: The problem was that the server( University project server which requires your uni login) required a login to access any file. That login started a session. When I wanted to start another session it just didn't worked. Solution you should use a server which is not requiring a login and than you can start your own session.

dben
  • 484
  • 1
  • 6
  • 21