We've retired the forum in favor of GitHub Discussions.

New conversations can be started on GitHub. Existing conversations will remain for a while longer.

Head over to GitHub →

PHP session not working

Max Glass July 19, 2019 by Max Glass

Hey hey,

I am working on something and have to use PHP sessions for that. The clue was, when clicking visiting a site, let's say host.com/page, an ID gets saved within a session. This works. But when I am going to host.com/page/subpage and echo the ID which got stored on host.com/page, it just does not work. So a variable gets set on /page and when the user gets redirected to /page/subpage the variable should be readable/transferred from /page to /page/subpage.

My layout.html looks like that:

<?php session_start();?>
<!DOCTYPE html>
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1">
...

Here is my partial.html, which is properly parsed within a template which is for host.com/page:

{{ if type == "partial" }}
    <?php
        $_SESSION["pID"] = $p->id;
        echo $_SESSION["pID"]; // This works nice and sturdy, so this session variable was saved
    ?>
{{ /if }}

And here is my template.html for host.com/page/subpage (PHP session should already got started by layout.html)

<?php
    echo $_SESSION["pID"]; // DOES NOT WORK
?>

In this very case the error is: ErrorException in Parser.php(1235) : eval()'d code line 3: Undefined index: pID

So it seems that the variable wasn't got saved in the session or the session wasn't saved.

I just don't know what to do on that one, hope one of you guys could help me out.

Cheers Max

Answered by Erin Dalzell!
>>>>>>> Answered <<<<<<<
7 Replies
3 Followers