By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,520 Members | 1,271 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,520 IT Pros & Developers. It's quick & easy.

PHP Session problem with AJAX

P: 3
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!
Jun 16 '07 #1
Share this Question
Share on Google+
5 Replies


acoder
Expert Mod 15k+
P: 16,027
Welcome to TSDN!

Can you post some code here.
Jun 16 '07 #2

P: 3
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

?>
Jun 16 '07 #3

ak1dnar
Expert 100+
P: 1,584
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
Jun 17 '07 #4

P: 3
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!
Jun 17 '07 #5

ak1dnar
Expert 100+
P: 1,584
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
Jun 18 '07 #6

Post your reply

Sign in to post your reply or Sign up for a free account.