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

  • Home
  • SEARCH
  • 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 6339547
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T19:40:09+00:00 2026-05-24T19:40:09+00:00

I am having trouble getting jQuery ajax to recognise a session. I am creating

  • 0

I am having trouble getting jQuery ajax to recognise a session. I am creating a php session from a login script, but what is happening is that when ajax loads the authenticated page, the session is always unset. For example, in the secure page, if I refresh the page, the session id changes each time. I have session_start(); in each page. Can someone please show me the correct way to handle sessions with ajax and php? I have spent 2 days and have used google so much, I will probably get an invite to there xmas lunch 🙂 I have included the relevant code and would be grateful for any help. Thanks

PS. If it makes any difference, I am trying to develop mobile app using jquery mobile.

login html js

$(function() {
    $("#kt_login1").click(function() {
        var user = $('#user').val();
        var pass = $('#pass').val();
        if (user == '') {
            $("#login_message").html('This field cannot be empty')
            $('label[for=user]').addClass("label")
            return false;
        }
        else if (pass == '') {
            $("#login_message").html('This field cannot be empty')
            $('label[for=pass]').addClass("label")
            return false;
        }
        else $('label[for=user]').removeClass("label");
        $('label[for=pass]').removeClass("label");

        //alert(user + pass + ok);
        data = 'user=' + user + '&pass=' + pass;
        $.ajax({
            type: "POST",
            url: "testajax.php",
            cache: false,
            data: data,
            success: function(data) {
                if (data == 'authenticated') {
                    //alert(user);
                    document.location = 'secure.php';
                }
                else $('#login_message').html('You are not authorised');

                //$(ok).val('Logged In');
                //$("#login").get(0).reset();
                //$("#form").dialog('close');
            },
            error: function(xhr, ajaxOptions, thrownError) {
                jAlert('There was an exception thrown somewhere');
                alert(xhr.status);
                alert(thrownError);
            }
        });
        return false;
    });
});

testajax.php

<?php

// test wether the user session is already set
$username = mysql_real_escape_string($_POST['user']);
$pass = mysql_real_escape_string(md5($_POST['pass']));

mysql_connect('localhost', 'root', '');
mysql_select_db('sample');

//now validating the username and password
$sql="SELECT * FROM user_usr WHERE username_usr='$username' and password_usr='$pass'";
$result=mysql_query($sql);
$row=mysql_fetch_array($result);

//if username exists
if(mysql_num_rows($result)>0) {
    session_start();
    $_SESSION['u_name']=$row['name_usr'];
    /*
    echo '<pre>';
    print_r( $_SESSION['u_name'] );
    print_r( $_REQUEST );

    echo '</pre>';
    exit;
     */
    echo 'authenticated';
}
else
{
  echo 'Unknown User';
}
?>

+++++SOLUTION+++++

Changed form input from submit to button and voila. All ok

  • 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-24T19:40:11+00:00Added an answer on May 24, 2026 at 7:40 pm

    you have to call session_start() each time working with a session (not only when creating it)

    see: http://php.net/manual/en/function.session-start.php

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

Sidebar

Related Questions

I'm having trouble getting a response back from a Jquery ajax call... (It's a
Creating a slideshow jQuery plugin, but having trouble getting it to work with more
I am having trouble getting inside my Search WebMethod from my JQuery call. Maybe
I'm having trouble getting jQuery's ajaxError global handler firing when I use a $.ajax
I am having trouble getting the contents of JSON object from a JQery.ajax call.
I'm having some trouble getting jQuery to play nice with DokuWiki - has anyone
Getting started with jquery and having trouble getting hello world type example going for
I'm having trouble getting the following to work in SQL Server 2k, but it
I am using HTML Purifier in my PHP project and am having trouble getting
I having trouble getting append() to work in safari for some reason, but it

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.