Connecting Tech Pros Worldwide Help | Site Map

PHP Session problem with AJAX

Newbie
 
Join Date: Jun 2007
Posts: 3
#1: Jun 16 '07
Hi all!
I can't use session variables with AJAX, I can't acces them and I can't initialize the session variables. They does'nt pass via XMLHttpRequest? If I don't pass the data via AJAX, everything is OK so it's not a syntax error. Somebody else having this problem? Please help me if you know something about this, I can't find any info about this problem. Thank for help!
acoder's Avatar
Site Moderator
 
Join Date: Nov 2006
Location: UK
Posts: 14,581
#2: Jun 16 '07

re: PHP Session problem with AJAX


Welcome to TSDN!

Can you post some code here.
Newbie
 
Join Date: Jun 2007
Posts: 3
#3: Jun 16 '07

re: PHP Session problem with AJAX


Hi, of course:

I have the folloving function, at authentification the login.php file is requested, in that file if the user data is ok, I would like to start a session, but that event does not occur. I have all on one page, the login form is replaced by the content of the page after the authentification was made.

The function which makes the request for the login.php :

function autentificare()
{
xmlHttp=GetXmlHttpObject();
if (xmlHttp==null)
{
alert ("Browser does not support HTTP Request")
return;
}
var url="./login.php"+"?andrascache="+new Date().getTime();
utilizator=document.form.nume_utilizator.value;
parola=document.form.parola.value;
xmlHttp.onreadystatechange=proceseazaLogarea;
xmlHttp.open("GET",url+'&nume_utilizator='+utiliza tor+'&parola='+parola,true);
xmlHttp.send();
}
function proceseazaLogarea()
{
if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
{
raspuns=xmlHttp.responseText;
if(raspuns=='eroare'){
document.getElementById('comentariu_p').innerHTML= '<b>Parola incorecta!<b>';
document.getElementById('comentariu_p').className= 'erori';
}
else
{
cerereContinut();
}

}
}

Below is the login.php content, for testing I let only
<?php
/*below the session should start, but in FireBug I can see that the session variable value is empty, as below:
Array

(

[nume_utilizator] =>

) */

session_start();
$utilizator=$_GET['nume_utilizator'];
$_SESSION['nume_utilizator']=$_utilizator;
print_r($_SESSION);
echo 'ok'; //authentification ok

?>
ak1dnar's Avatar
Moderator
 
Join Date: Jan 2007
Location: Colombo
Posts: 1,439
#4: Jun 17 '07

re: PHP Session problem with AJAX


Quote:

Originally Posted by andrasnet

Hi, of course:

I have the folloving function, at authentification the login.php file is requested, in that file if the user data is ok, I would like to start a session, but that event does not occur. I have all on one page, the login form is replaced by the content of the page after the authentification was made.

The function which makes the request for the login.php :

function autentificare()
{
xmlHttp=GetXmlHttpObject();
if (xmlHttp==null)
{
alert ("Browser does not support HTTP Request")
return;
}
var url="./login.php"+"?andrascache="+new Date().getTime();
utilizator=document.form.nume_utilizator.value;
parola=document.form.parola.value;
xmlHttp.onreadystatechange=proceseazaLogarea;
xmlHttp.open("GET",url+'&nume_utilizator='+utiliza tor+'&parola='+parola,true);
xmlHttp.send();
}
function proceseazaLogarea()
{
if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
{
raspuns=xmlHttp.responseText;
if(raspuns=='eroare'){
document.getElementById('comentariu_p').innerHTML= '<b>Parola incorecta!<b>';
document.getElementById('comentariu_p').className= 'erori';
}
else
{
cerereContinut();
}

}
}

Below is the login.php content, for testing I let only
<?php
/*below the session should start, but in FireBug I can see that the session variable value is empty, as below:
Array

(

[nume_utilizator] =>

) */

session_start();
$utilizator=$_GET['nume_utilizator'];
$_SESSION['nume_utilizator']=$_utilizator;
print_r($_SESSION);
echo 'ok'; //authentification ok

?>

Expand|Select|Wrap|Line Numbers
  1. <?php
  2. session_start();
  3. $utilizator=$_GET['nume_utilizator'];
  4.  
  5. # $_SESSION['nume_utilizator']=$_utilizator; # what is this underscore on $_utilizator 
  6. $_SESSION['nume_utilizator']=$utilizator;
  7. if($_SESSION['nume_utilizator']){
  8. echo 'ok';
  9. }else{
  10. echo 'Noap';
  11. }
  12. ?>
  13.  
This may help a lot for us, if you read it before your next post.
Full list of Code Tag Supported Languages

Thanks
-Ajaxrand
Newbie
 
Join Date: Jun 2007
Posts: 3
#5: Jun 17 '07

re: PHP Session problem with AJAX


Thank you for your reply!
Indeed, that underscore was the problem, sorry for disturbing with problems like these, I was too tired when I typed that code :) Next time I wil use the code tags round the code.
Thank you!
ak1dnar's Avatar
Moderator
 
Join Date: Jan 2007
Location: Colombo
Posts: 1,439
#6: Jun 18 '07

re: PHP Session problem with AJAX


Quote:

Originally Posted by andrasnet

Thank you for your reply!
Indeed, that underscore was the problem, sorry for disturbing with problems like these, I was too tired when I typed that code :) Next time I wil use the code tags round the code.
Thank you!

Hi andrasnet,

You are welcome anytime here @ TSDN.
Hope to c u again.

Thanks,
-Ajaxrand
Reply