PHP Session Lost After Refresh

Discussion in 'Programming/Scripts' started by Bob C, Oct 15, 2018.

  1. Bob C

    Bob C New Member

    I have a simple website I'm working on for a community chorus. It consists of a home PHP page with two (2) additional PHP pages used for AJAX calls as well as a javascript file and css styling file. In my home page, the very first command is "session_start();" There are no other session commands on this page. The onload command for the body of my home page makes an AJAX call to populate fields in my home page. This AJAX call calculates and assigns a value to the session variable for the number of songs (i.e., $_SESSION["songNumber"]). Once loaded, my home page contains a login for chorus members to access restricted materials. This login is sent to a third php page used as an AJAX call to verify the password used. This third php page assigns a value to a second session variable ($_SESSION["pwAccepted"]) which indicates whether the password used was accepted as correct or not. If the password is correct, additional materials are displayed. At present, there is no logoff function that would destroy or end the session. Here's a summary of the coding flow:
    1. Home page starts session.
    2. Onload of home page body initiates an AJAX call through a javascript function to populate fields in the home page. It also calculates and assigns a value to a session variable ($_SESSION["songNumber"]). This means that every time the home page body is loaded (or refreshed), this AJAX call is made and the session variable ($_SESSION["songNumber"]) is recalculated and reassigned.
    3. Once the home page is fully loaded, chorus members are asked to login to access additional restricted materials. The login information is passed through a javascript function to another AJAX call to a third php file which verifies and sets another session variable ($_SESSION["pwAccepted"]). This means that this session variable ($_SESSION["pwAccepted"]) is assigned (or reassigned) only when the button activating the javascript is pressed.
    4. The session is never ended or destroyed and variables are never unset.
    My problem is that when I refresh the main page, my session variable ($_SESSION["pwAccepted"]) is lost but I want chorus members to be able to see the additional materials without having to login again. What am I doing wrong?
  2. Bob C

    Bob C New Member

    Please ignore this post. I've decided to use cookies instead of session.
  3. ifour.parth

    ifour.parth New Member

    header("Location: index.php");

    if ($loginNumRows == 1) {
    $_SESSION['username'] = $loginRow['name'];
    header("Location: index.php");

Share This Page