PHP session only works after a refresh

Posted on 2005-04-07
Last Modified: 2006-11-18
I am using sessions with multiple php pages.  The session variables do not work until The second time I view the page (i.e. refresh)

I tried a simple version of sessions and had the same results.

$_SESSION ["var"] = "val";
echo $var;

the echo for both the $var and the $PHPSESSID do not show up until I refresh the page.  How do I use sessions without refreshing?
Question by:jimfrith
    LVL 7

    Accepted Solution

    refer to your session variables through the $_SESSION[] superglobal array.  And use session_id() to echo out the session's ID:

    $_SESSION ["var"] = "val";
    echo $_SESSION["var"] . "<br />\n";
    echo session_id() . "<br />\n";
    LVL 7

    Expert Comment

    and put your opening <?php tag in the right place ;)

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Join & Write a Comment

    Consider the following scenario: You are working on a website and make something great - something that lets the server work with information submitted by your users. This could be anything, from a simple guestbook to a e-Money solution. But what…
    Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
    This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
    The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

    730 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    15 Experts available now in Live!

    Get 1:1 Help Now