I have been messing around with adding sessions and I got everything to work. I can have users login, and it keeps track of all of their info through session_start(). However there is one strange thing that is bothering me. If someone is logged into the page, then if they leave and come back it appears they are not logged in for the first loading of the page. However if I click any of the links (which are stored on the same server), sure enough all their user information comes back up. I've sifted through my code and checked and it just doesn't keep track of their session id for the first page load after leaving the website. But when the page is reloaded it has their session id again.

I don't know a lot about how it works so If anyone could point me in the right direction, or let me know if this is even possible to fix. I have been trying to avoid cookies, but could use one to hold session id if I really had no other choice.

(I tried page redirection to the same page, and it worked. But I know some browsers such as chrome don't allow users to go to websites that redirect to the same server.)

Recommended Answers

All 2 Replies

Use the following in all pages.....

  1. session_start() in the first line after <?php
  2. check if session exists using if condition as below:

      if(isset($_SESSION["your_session_variable"]))
           {
              do all coding within this block;
           }
      else
           {
              display login page;
           }   
    

The login page can be displayed as below:

  header("Location:your_loginpage_address");
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.