2

I have created a php session and have assigned data like so

session_start();
$_SESSION['desd']=$imgD;

When i try to retrieve it on other page i get nothing.

this is what i have done on second page,

session_start();
$_imgname = $_SESSION['desd'];

How to do that?

MJQ
  • 1,700
  • 6
  • 33
  • 58

2 Answers2

3

The code is valid, assuming that the variable $imgD is defined (var_dump is one way of checking this, but print or echo may also work).

Check to ensure that cookies are enable in your browser.

Also, be sure that session_start() comes near the top of your script, it should be the first thing sent to the client each time.

To test a session, create "index.php" with the following code:

<?php
  session_start();  
  if(isset($_SESSION['views']))
      $_SESSION['views'] = $_SESSION['views']+ 1;
  else
      $_SESSION['views'] = 1;

  echo "views = " . $_SESSION['views']; 
?>

Reload the page several times and you should see an incrementing number.

An example of passing session variables between two pages is as follows:

PageOne.php

 <?php 
   session_start(); 

   // makes an array 
   $colors=array('red', 'yellow', 'blue'); 
   // adds it to our session 
   $_SESSION['color']=$colors; 
   $_SESSION['size']='small'; 
   $_SESSION['shape']='round'; 
   print "Done";
 ?> 

PageTwo.php

<?php 
 session_start(); 
 print_r ($_SESSION);
 echo "<p>";

 //echo a single entry from the array
 echo $_SESSION['color'][2];
?>

For simplicity put PageOne.php and PageTwo.php in the same directory. Call PageOne.php and then PageTwo.php.

Try also tutorials here, here, and here.

Richard
  • 44,865
  • 24
  • 144
  • 216
  • 1
    And remember that variables passed to session are available **after** page reload. – Peon Sep 20 '12 at 14:11
  • Ok. If I enable cookies in my browser and it works. then what about client side. If client has cookies disabled then my code will not work! – MJQ Sep 20 '12 at 14:15
  • You're right, it won't. But you can try to [detect that](http://stackoverflow.com/questions/531393/how-to-detect-if-cookies-are-disabled-is-it-possible). But the question of what to do about it is separate from the question you've posed here. – Richard Sep 20 '12 at 14:16
  • 1
    But, you could try looking [here](http://stackoverflow.com/questions/9528838/what-happens-if-cookies-are-disabled). – Richard Sep 20 '12 at 14:18
  • Bdw! I sent data through form post request and it went well. Does that mean cookies are enabled?? – MJQ Sep 20 '12 at 14:20
  • Statistically, most browsers/users will accept your cookies. See [http://smorgasbork.com/component/content/article/84-a-study-of-internet-users-cookie-and-javascript-settings](here) and [http://www.statowl.com/third_party_cookie_support.php](here). – Richard Sep 20 '12 at 14:21
  • I am using e-commerce solution. Magento! and it uses session itself. So cookies are enabled!!! – MJQ Sep 20 '12 at 14:22
  • No, @MJQ. POSTs and GETs are separate and not session-dependent. To determine if cookies are enabled, you will need to check your browser or generate some test code for sessions. – Richard Sep 20 '12 at 14:22
  • If cookies are not enabled in my browser then how am i using stack overflow and going through different pages while still logged in??? So, cookies are enabled!!! – MJQ Sep 20 '12 at 14:25
  • Ok. It is incremented. So, it means my session is being created but i can't access the values from other page!!!! – MJQ Sep 20 '12 at 14:31
  • If you looked through the links I put in the answer, you would find that the third link's second page contains an example of your situation. A direct link is [here](http://php.about.com/od/advancedphp/ss/php_sessions_2.htm). – Richard Sep 20 '12 at 14:35
  • Ok. I have tried the same code as you have posted. But I didn't get the data in the second page!!! :( – MJQ Sep 20 '12 at 14:43
  • If the numbers increment in my single page example, but the two page example does not work, then I'm not sure what the problem is. You could make a new question containing both examples and ask what might cause the difference, but without further tests or debugging efforts on your part, I'm afraid we'd simply be taking random guesses at this point. – Richard Sep 20 '12 at 14:50
  • Ok. Thanks for your response and effort. I'm accepting your answer though! – MJQ Sep 20 '12 at 14:54
1

If you call session_start(), the server gets the session ID from cookies.

Maybe something is wrong with the cookies because the code is valid.

rationalboss
  • 5,225
  • 3
  • 28
  • 48
Yegor Lukash
  • 492
  • 2
  • 18