开发者

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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜