Author Topic: Set session with ajax and php  (Read 1563 times)

Offline FrankBro

  • Level 8
  • *
  • Posts: 39
  • Reputation: +0/-0
    • View Profile
Set session with ajax and php
« on: May 18, 2010, 11:38:32 PM »
So I'm trying to get a ajax call to set a session for a login system. I know javascript can't set sessions, that's why I'm calling a php page via a get. If I just load the php page called from javascript, it's working. But it's not if I call it via ajax. Any idea?

The js.
Code: [Select]
function getCharSession(id)
{
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
var test = xmlhttp.responseText;
if(test = "success")
window.location = "game.php";
}
}
xmlhttp.open("GET","char.php?q="+id,true);
xmlhttp.send();
}

the php
Code: [Select]
<?php
require_once(dirname(__FILE__) . '/../config.php');
require_once(
dirname(__FILE__) . '/../classes/class.factory.php');

try
{
session_start();
$charid=$_GET["q"];
if($charid == '' || !is_numeric($charid))
throw new GeneralException('The supplied character is not valid.');
else
{
$db = new DB();

$factory = new Factory($db);
$user $factory -> createNewUser();
$user ->  isCharUnderUser($_SESSION['uid'], $charid);

$_SESSION['cid'] = $charid;
$_SESSION['hash'] = md5(mysecretkey $_SESSION['uid'] . $_SESSION['cid'] . $_SERVER['REMOTE_ADDR']);
echo 'success';
}
}
catch(
GeneralException $charException)
{
$charException -> displayMessage();
}

?>

I've been working on that for a while, really can't figure it out. The call to the function is on a onclick.
The condition from the javascript is met,
Code: [Select]
var test = xmlhttp.responseText;
     if(test == "success")
          window.location = "game.php";
I am being redirected .. the problem is the the session isn't set.
« Last Edit: May 21, 2010, 03:57:27 PM by FrankBro »

Offline FrankBro

  • Level 8
  • *
  • Posts: 39
  • Reputation: +0/-0
    • View Profile
Re: Set session with ajax and php
« Reply #1 on: May 21, 2010, 12:03:17 AM »
really? nobody?

Offline aerosuidae

  • Level 9
  • *
  • Posts: 50
  • Reputation: +5/-0
    • View Profile
    • Return to Sol
Re: Set session with ajax and php
« Reply #2 on: May 21, 2010, 02:30:07 AM »
var test = xmlhttp.responseText;
   if(test = "success")
      window.location = "game.php";

Should that be a "==" ?

I'm not sure how Javascript is supposed to behave with variable assignment in conditional statements, but if it is like PHP I think the above would always be true?  Perhaps someone more knowledgable on JS knows...

Offline FrankBro

  • Level 8
  • *
  • Posts: 39
  • Reputation: +0/-0
    • View Profile
Re: Set session with ajax and php
« Reply #3 on: May 21, 2010, 11:40:31 AM »
O wow. This ain't the first time I do that ....
Well it was always true since it was an assigment. So that means the ajax response isnt even working. Meh. Any idea?

Offline dsheroh

  • Level 21
  • *
  • Posts: 235
  • Reputation: +6/-0
  • Perl Vicar
    • View Profile
    • Psi Rangers
Re: Set session with ajax and php
« Reply #4 on: May 21, 2010, 03:13:16 PM »
Didn't answer before because I don't really do much PHP, but a quick google for "set cookie in ajax get" turned up http://www.sitepoint.com/forums/showthread.php?t=627717 in which someone was having a similar problem.  His issue ended up being that he needed to explicitly include an expiration time and path in his call to setcookie().  Looking over your posted PHP code, though, I don't see a call to setcookie() at all, which would explain why no cookie is being set.  (Unless one of the functions you're calling is doing a setcookie() internally...  Like I said, I don't do much PHP, so I don't know how they behave.)

Offline FrankBro

  • Level 8
  • *
  • Posts: 39
  • Reputation: +0/-0
    • View Profile
Re: Set session with ajax and php
« Reply #5 on: May 21, 2010, 03:44:55 PM »
I'm not setting cookies at all, I'm setting a session. Sessions are set by the server(php), cookie by the browser(javascript). Not sure I trust cookie like i trust sessions.

Offline FrankBro

  • Level 8
  • *
  • Posts: 39
  • Reputation: +0/-0
    • View Profile
Re: Set session with ajax and php
« Reply #6 on: May 21, 2010, 04:34:24 PM »
fixed, nvm im a noob =D
firebug FTW

Offline dsheroh

  • Level 21
  • *
  • Posts: 235
  • Reputation: +6/-0
  • Perl Vicar
    • View Profile
    • Psi Rangers
Re: Set session with ajax and php
« Reply #7 on: May 22, 2010, 11:48:00 AM »
I'm not setting cookies at all, I'm setting a session. Sessions are set by the server(php), cookie by the browser(javascript). Not sure I trust cookie like i trust sessions.
You're almost certainly setting a cookie.  How do you think the session is associated with the user?

There are three ways to track a session: URI parameter, hidden form input, or cookies.  Of those three options, the first is fundamentally incompatible with AJAX (you can't change the URI in the location bar to add the session id without doing a full page load) and the second is of limited use in general (it only preserves the session across form submits and will lose it if you click a regular link), which pretty much leaves cookies as the only viable way to establish a session via an AJAX request.

Sessions are stored on the server and the browser never sees them.  Cookies are set by the server and stored by the browser, which returns them with each request.

Offline raestlyn

  • Level 29
  • **
  • Posts: 464
  • Reputation: +9/-5
    • View Profile
Re: Set session with ajax and php
« Reply #8 on: May 23, 2010, 04:11:49 PM »
Actually there is 4th way to store session-like data, but it is just for Javascript. Window.name property can hold up up to 2 megabits of information (In Safari, In IE its 64 and FF around 12).


I can send you pics of my cocks if you want reference.


Offline dsheroh

  • Level 21
  • *
  • Posts: 235
  • Reputation: +6/-0
  • Perl Vicar
    • View Profile
    • Psi Rangers
Re: Set session with ajax and php
« Reply #9 on: May 24, 2010, 12:35:03 PM »
Actually there is 4th way to store session-like data, but it is just for Javascript. Window.name property can hold up up to 2 megabits of information (In Safari, In IE its 64 and FF around 12).
Interesting...  I'd never heard of that before.  Thanks!

Offline raestlyn

  • Level 29
  • **
  • Posts: 464
  • Reputation: +9/-5
    • View Profile
Re: Set session with ajax and php
« Reply #10 on: May 24, 2010, 12:44:19 PM »
Actually there is 4th way to store session-like data, but it is just for Javascript. Window.name property can hold up up to 2 megabits of information (In Safari, In IE its 64 and FF around 12).
Interesting...  I'd never heard of that before.  Thanks!
You can read it a bit in this blog post. The lib it advertises isn't sophisticated, but it'll work.


I can send you pics of my cocks if you want reference.


 


SimplePortal 2.3.3 © 2008-2010, SimplePortal