Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7079761
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T06:42:42+00:00 2026-05-28T06:42:42+00:00

Can somebody explain to me where I’m going wrong with this login setup? I’ve

  • 0

Can somebody explain to me where I’m going wrong with this login setup? I’ve outlined the process below and included the code I am using. Everything works except for the fact that session variables never seem to get set.

The page I am forwarding them to dumps out the session variables onto the page, except by the time you get to the next page, the session variable doesn’t seem to exist. If I actually return the $_SESSION variable as part of the json response to the AJAX call, it returns the proper values, but as soon as the page changes, the values seem to go away.

Process:

  1. Login Form Submitted

  2. AJAX call made to login.php

  3. login.php checks username and password against the database.
    If the user is found, sets $_SESSION variables for the session,
    runs some other SQL and returns success, if not returns the appropriate error.

  4. AJAX function receives result from login.php, if success forwards user to
    customer page, if not shows error from result.

Actual Code:

jQuery AJAX call made by login form:

$("#login_form_header").submit(function(event){

    event.preventDefault();

    $.ajax({
        url: 'xhr/login.php',
        data: $(this).serialize(),
        type: 'post',
        dataType: 'json',
        success: function(result){
            if (result.success){
                window.location = "logged.php";
                return false;
            };
        },
        error: function(e){console.log("Could not retrieve login information")}
    });

    return false;
});

Login.php:

<?PHP   

# Start the user session
if(!isset($_SESSION)) {
    session_start();
};

# Make sure form data was passed to the script
IF (isset($_POST['username']) && isset($_POST['password'])){

    # Connect to the database
    REQUIRE('../../../../db_oystrr.php');

    # Define variables
    $given_username = $_POST['username'];
    $given_password = $_POST['password'];
    $given_username = stripslashes($given_username);
    $given_password = stripslashes($given_password);
    $given_username = mysql_real_escape_string($given_username);
    $given_password = mysql_real_escape_string($given_password);
    $matched_username = "";
    $matched_password = "";


    # See if there is matching info in the database
    $sql = 'SELECT username, pass FROM users WHERE username="'.$given_username.'"';
    $result = mysql_query($sql);
    while($row = mysql_fetch_assoc($result)){
        $pass_hash = *********;
        if ($pass_hash == $row['pass']){
            $matched_username = $row['username'];
            $matched_password = $row['pass'];
        };
    };


    # If there was a match
    IF ($matched_username != "" && $matched_password != ""){

        # If there is only one result returned
        $session_sql = 'SELECT * FROM users WHERE username="'.$matched_username.'" AND pass="'.$matched_password.'";';
        $session_result = mysql_query($session_sql);
        $returned_row = mysql_fetch_assoc($session_result);
        $user_check = mysql_num_rows($returned_row);

        IF(count($user_check) > 0  &&  count($user_check) < 2){

            # Set our session values
            $_SESSION['id'] = $returned_row['id'];
            $_SESSION['last_login'] = $returned_row['last_login'];
            $_SESSION['username'] = $returned_row['username'];
            $_SESSION['signup_date'] = $returned_row['signup_date']; 

            session_write_close();

            # Set users last login date and time and re-hash their password to this login
            $this_login = **********;
            $hashed_password = **********;
            $update_sql = '************';
            mysql_query($update_sql);

            echo json_encode(array("success"=>"user logged in", "session"=>$_SESSION));
        }ELSE 
            echo json_encode(array("error"=>"More than one user with the same information.  What did you do?!"));
    }ELSE
        echo json_encode(array("error"=>"Invalid login provided."));
}ELSE
    echo json_encode(array("error"=>"You must enter a username and Password."))

?>

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T06:42:42+00:00Added an answer on May 28, 2026 at 6:42 am

    Solution:

    PHP Session needs to be started on every page you need access to the session variable, unless PHP is configured to auto-start sessions. Thanks Marc B and nunespascal.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Can somebody explain me why this T-SQL code only returns one row with the
Can somebody explain me why the small piece of code doesn't work? This is
Can somebody explain me what I am doing wrong? This is teh error I
enter code here hello, everyone, can somebody explain what is my problem in this
Can somebody explain why the following code is not valid? Is it because the
I'm running a Django app using FastCGI and lighttpd. Can somebody explain me what
Can somebody explain me why the following piece of code fails to compile. The
Can somebody explain the following code please? #if 1 // loop type #define FOR_IS_FASTER
Can somebody explain to me why this works (in Python 2.5) : class Foo(object):
Can somebody explain what is the exact reason behind using Action<T> and Predicate<T> as

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.