Forum Moderators: open

Message Too Old, No Replies

AJAX and PHP Session Info

         

ahmedtheking

5:19 pm on Feb 11, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



This may be the wrong place, but it's a bit of both. So, would AJAX be able to send PHP session info if a request was sent like this: (using prototype lib)

new Ajax.Request ('POST','http://webmasterworld/somefile.php?<?=SID;?>',{ .... });

scriptmasterdel

5:50 pm on Feb 11, 2007 (gmt 0)

10+ Year Member



Sure, you can use ajax to pass whatever you like to a php page and then on the php page you need to initiate the session and then store the variable and then it can be used like you would normally use a session variable.

e.g.

If you wanted to post the users age then in the PHP file you would do somthing liken the following:

<?
session_start();

if ($_POST['age'])
{
$_SESSION['age'] = $_POST['age'];
}
?>

Like i said, the session variable is now stored for any other use.

I hope i have helped.

Del

scriptmasterdel

5:54 pm on Feb 11, 2007 (gmt 0)

10+ Year Member



Incase i jumped the gun a little then ..

You can pass the information from the url, but usualy you would send them as a string in your request, not as a direct url.

Read more here: wikipedia xmlHTTPRequest [en.wikipedia.org]

ahmedtheking

6:10 pm on Feb 11, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Ah ok good times. So not [lalala...] yeah?

Tastatura

6:11 pm on Feb 11, 2007 (gmt 0)

10+ Year Member



Not sure if this is suitable for what you want, but alternatively you can write SID (or any other info) to cookie using JS and have PHP read it.

[edited by: Tastatura at 6:12 pm (utc) on Feb. 11, 2007]

ahmedtheking

6:12 pm on Feb 11, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Wait wait wait...

Now with PHP sess, they're either cookies or in the URL right. Now, if they're cookies, then there's no URL trail. So, because of that, how can I send it as a var?

ahmedtheking

6:14 pm on Feb 11, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Sorry, stupid me! Ok here it is:

Because AJAX is just a continuation, so to say, of the browser but in the background, all session data is carried without it being declared!