简体   繁体   中英

creating image with php problem

my index.php file i have somethingk like :

<?php
   session_start();
   $_SESSION[some_value] = 1;
?>
<img src="image.php" alt="some image"/>
<?php
   $_SESSION[some_value] = 0;
?>

my image.php file i have look like ( basic code ) :

<?php
  session_start();
  header("Content-Type: image/png");
  $im = @imagecreate(400, 20)
  or die("Cannot Initialize new GD image stream");
  $background_color = imagecolorallocate($im, 255, 255, 255);
  $text_color = imagecolorallocate($im, 233, 14, 91);
  imagestring($im, 1, 5, 5,"session value is : {$_SESSION[some_value]}", $text_color);
  imagepng($im);
  imagedestroy($im);
?>

Now, when i load my index.php page in browser the value in image is session value is : 0 , how to make it to show 1 and then code in index.php to set it to 0 ( adding in image.php code to set value to 0 is not what i'm looking for )

You run into problems because in your code both scripts do not share the memory of the session even if both are using the $_SESSION array.

That's by the nature of how and when PHP stores the values of the $_SESSION array.

Instead you need a shared store like a database or shared memory to exchange values between your scripts.

The problem is that the browser loads image.php after index.php is loaded.

You can do something like this:

<img src="image.php?some_value=<?php echo $_SESSION[some_value]; ?>" alt="some image"/>

But it depends on your particular purpose.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM