php, mySQL & AJAX: Unable to use sessions across the scripts in the same domain

Posted by Devner on Stack Overflow See other posts from Stack Overflow or by Devner
Published on 2010-05-08T18:17:20Z Indexed on 2010/05/08 18:18 UTC
Read the original article Hit count: 273

Filed under:
|
|

Hi all,

I have the following pages: page1.php, page2.php and page3.php. Code in each of them is as below

CODE:

page1.php

<script type="text/javascript">

$(function(){
    $('#imgID').upload({
        submit_to_url: "page2.php",
        file_name: 'myfile1',
        description : "Image",
        limit : 1,      
        file_types : "*.jpg",

    })
}); 

</script>

<body>
<form action="page3.php" method="post" enctype="multipart/form-data" name="frm1" id="frm1">
//Some other text fields
  <input type="submit" name="submit" id="submit" value="Submit" />
</form>

</body>

page2.php

<?php session_start();
$a = $_SESSION['a'];
$b = $_SESSION['b'];
$c = $_SESSION['c'];

$res = mysql_query("SELECT col FROM table WHERE col1 = $a AND col2 = $b AND col3 = $c LIMIT 1");
$num_rows = mysql_num_rows($res);
echo $num_rows; //echos 0 when in fact it should have been 1 because the data in the Session exists.

//Ok let's proceed further
//... Do some stuff...
//Store some more values and create new session variables (and assume that page1.php is going to be able to use it)
$_SESSION['d'] = 'd';
$_SESSION['e'] = 'e';
$_SESSION['f'] = 'f';

if (move_uploaded_file($_FILES['file']['tmp_name'], $file)) 
{ 
  echo "success"; 
} 
else 
{
 echo "error ".$_FILES['file']['error'];
}

?>

page3.php

<?php session_start(); 
if( isset($_POST['submit']) )
{
  //These sessions are non-existent although the AJAX request 
  //to page2.php may have created them when called via AJAX from within page1.php 
  echo $_SESSION['d'].$_SESSION['e'].$_SESSION['f']; ?> 
}
?>
  1. As the code says it I am posting some info via AJAX call from page1.php to page2.php. page2.php is supposed to be able to use the session values from page1.php i.e. $_SESSION['a'], $_SESSION['b'] and $_SESSION['c'] but it does not. Why? How can I fix this?

  2. page2.php is creating some more sessions after some processing is done and a response is sent back to page1.php. The submit button of the form on page1.php is hit and the page gets POST'ed to page3.php. But when the SESSION info that gets created in page2.php is echoed, it's blank signifying that SESSIONS from page2.php are not used. How can I fix this?

I looked over a lot of information and have spent about 50 hours trying to do different things with my scripts before arriving at the above conclusions. My app. is custom made using function (not OOPS) and does not use any PHP frameworks & I am not even about to use any as my knowledge of OOP concepts is limited any many frameworks are object oriented. I came across race conditions, but the solutions provided don't help too much. One more solution of using DB to hold sessions and seek and retrieve from DB is the last thing on my mind and I really want to avoid creating table, coding and maintaining code for a task as simple as just keeping sessions across pages in the same domain.

So my request is: Is there a way that I can solve the above problem(s) via simple coding in present conditions? Any help is appreciated.

Thank you.

© Stack Overflow or respective owner

Related posts about php

Related posts about mysql