views:

37

answers:

2

i am creating session while rendering the page. After that i am calling the AJAX page every 4 seconds. the AJAX page doesnt gettign the Session information.

What may be the reason. Whether ajax requests dont maintain Session information?

Code:

Normal HTML

<body onload="timerint(11)">
<div style="display:none;">
    <button id="prev"><<</button>
    <button id="next">>></button>
</div>

<div class="anyClass">
    <ul id="news-ul">
        <?php 
          include('render_latest.php');
        ?>
    </ul>
</div>
</body>

render_latest.php

<?php
session_start(); 
$con = mysql_connect("localhost","root","root");
mysql_select_db("test_db", $con);
$i=1;

$lastresult=mysql_query("SELECT MAX(epoch) as latestepoch FROM list_data");

while($row = mysql_fetch_array($lastresult))
  {
    //$_session['lastepoch'] =  $row['latestepoch'] ;
    $_session['lastepoch']=12345;

  }

$result=mysql_query("SELECT * FROM list_data order by epoch desc LIMIT 4 ");
while($row = mysql_fetch_array($result))
  {
    echo '<li>';
    echo $row['list_item'] . '<br/>' ;
    echo $row['epoch'] . '<br/>';
    echo $_session['lastepoch'];
    echo '</li>';
  }

?> 

AJAX Page

<?php
session_start(); 
$t=$_SESSION['lastepoch'];
$con = mysql_connect("localhost","root","root");
mysql_select_db("test_db", $con);

$result=mysql_query("SELECT * FROM list_data order by epoch desc LIMIT 1 ");

while($row = mysql_fetch_array($result))
  {
    echo '<li>';
    echo $t;
    echo $row['list_item'] ;
    echo '</li>';
  }

?> 
A: 

The session data are stored usually in cookies for all browsers, or you can use the query string of your request to pass parameters. eg: .../render_latest.php?lastepoch=123456 and read the lastepoch parameter in your php.

Or you can use HTML5 client-side storage in modern browsers.

Mic
Session data is not stored in cookies. The cookie only stores the session id and the actual data is on the server. This guy's problem is though that the session data is not being stored at all for some reason.
Slavic
@slavic, May be it isn't clear, but I wanted to say that when you start using ajax, you are usually stateless and you store session data if needed client side.
Mic
One could surely delegate some of the storing ability to the browser, but not in the case of usernames and passwords, for example.
Slavic
Obviously, but here it was about a date. And I'm not sure storing user/pwd in session is a good idea either.
Mic
+1  A: 

Might be a typo in render_latest.php. Your'e using $_session instead of $_SESSION

Alex Regenass