Jump to content

AJAX with PHP Sessions


AndrewM16921

Recommended Posts

Hey so, I'm trying to get a php session to work with ajax calls from jquery. It doesn't seem to be working, so I'm guessing something like a session id isn't getting sent by the ajax calls. Not sure how to fix this. Googled some stuff, wasn't entirely sure what to make of it all. I'll post some examples I whipped up to test it. Pretty simplistic example. The js below gets "info.php", which should return a couple spans, the first of which (for testing sake) should just increment each time it's called.

$(function(){  $("#sp-info").load("info.php");}

<?php  session_start();  if(isset($_SESSION["uid"]))  {	$_SESSION["uid"] = 1;  }  else  {	$_SESSION["uid"] = $_SESSION["uid"] + 1;  }?><span id="info-left">  Welcome, <? echo $_SESSION["uid"]; ?>!</span>

Link to comment
Share on other sites

Don't start the session for the first time in the ajax call, start the session on the parent page first so that they have their session cookie already by the time they start sending ajax requests.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...