I know it looks like a banal question but please read the whole thing, I am stumped by this.
I have an AJAX call on one of my pages, it’s a dynamic messaging system:
function validateMessage(){
var recipient = document.getElementById("send_to").value;
var subject = document.getElementById("popup_subject").value;
var message = document.getElementById("popup_message").value;
var parameters="message="+message+"&recipient="+recipient+"&subject="+subject;
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)
{
document.getElementById("error_mess").innerHTML = xmlhttp.responseText;
}
}
xmlhttp.open("POST","include/send_message.php",false);
xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded")
xmlhttp.send(parameters);
}
It’s implemented as synchronous for a reason, that’s not the issue here. I tried switching to asynchronous and the problem remains.
This is the send_message.php file, it just grabs the POST variables and saves them into the database:
<?php
session_start();
include('db.php');
dbConnect();
$message=$_POST['message'];
$subject=$_POST['subject'];
$recipient=$_POST['recipient'];
$result=mysql_query("select * from korisnici where username='$recipient' ") or die(mysql_error());
$row=mysql_fetch_array($result);
$num=mysql_num_rows($result);
if($recipient=="Poruka za..." || $subject=="Naslov..." || $message=="Poruka" || $recipient=="" || $subject=="" || $message=="")
echo "<p style='color:red;'>Morate popuniti sva polja.</p>";
elseif($num==0)
echo "<p style='color:red;'>Korisnik ne postoji.</p>";
else{
$prima=$row['id_user'];
$salje=$_SESSION['id_user'];
mysql_query("insert into poruke (salje, prima, naslov, poruka)
values ('$salje', '$prima', '$subject', '$message') ") or die(mysql_error());
echo "<p style='color:green;'>Poruka uspješno poslata!</p>";
}
?>
However, when I tried to save the $_SESSION[‘id_user’] variable (as the sender) I found a problem – it turns out the session is being destroyed every time this AJAX call runs! So doing print_r($_SESSION) right after session_start() prints an empty array.
The session is alive on the original page itself, and refreshing that page keeps the session alive. Only when I click the button to make the AJAX call, the session disappears. Can someone spot the issue?
Here’s the solution, as given to me by he technical support team of my hosting provider:
Set suhosin.session.encrypt=Off in /home/username/public_html
Set suPHP_ConfigPath /home/username/public_html in .htaccess.
And that’s it, works now. No time to investigate these settings at this point, but I hope this will help someone in the same situation.