alhena Posted October 29, 2008 Share Posted October 29, 2008 I have a script that generates an image, and I need to pass a variable to it. The catch? I don't want to put the variable into the URL, because it can be too easily manipulated. The relevant code looks something like this: for ($i = 1; $i <= $j; $i++){ echo '<img src="image.php"> '; } And I want the image.php script to know what value $i has. I have tried using a temporary session variable, but the image script only recognizes the one that was last set. For example, if the for-loop runs five times, I'd get five images, all of them with 5 as the variable, which is wrong because it should (obviously) go from 1-5. Any ideas how to solve this? Link to comment https://forums.phpfreaks.com/topic/130550-solved-passing-a-variable-to-an-image-generating-script/ Share on other sites More sharing options...
bobbinsbro Posted October 29, 2008 Share Posted October 29, 2008 first idea that comes to my mind is to have: $fh = fopen('loopIteration.txt', 'w'); $fwrite($fh, $i); $fclose($fh); inside the for loop (before you echo the img tag), and then read the contents of loopIteration.txt in image.php. very likely there is a better way of doing this, but you can use this until something better is suggested. Link to comment https://forums.phpfreaks.com/topic/130550-solved-passing-a-variable-to-an-image-generating-script/#findComment-677416 Share on other sites More sharing options...
Prismatic Posted October 29, 2008 Share Posted October 29, 2008 <?php for ($i = 1; $i <= $j; $i++){ $_SESSION['iter'] = $i; echo '<img src="image.php"> '; } ?> <?php //image.php $i = $_SESSION['iter']; unset($_SESSION['iter']); //other stuff ?> Link to comment https://forums.phpfreaks.com/topic/130550-solved-passing-a-variable-to-an-image-generating-script/#findComment-677445 Share on other sites More sharing options...
rhodesa Posted October 29, 2008 Share Posted October 29, 2008 there really isn't a "good" way to do it...i would just pass it via the URL and validate the data you are receiving to make sure it's within the parameters you expect. Link to comment https://forums.phpfreaks.com/topic/130550-solved-passing-a-variable-to-an-image-generating-script/#findComment-677462 Share on other sites More sharing options...
alhena Posted October 30, 2008 Author Share Posted October 30, 2008 Thank you all for your replies! I think I will go ahead and just do the double-checking of the data in the image script. Link to comment https://forums.phpfreaks.com/topic/130550-solved-passing-a-variable-to-an-image-generating-script/#findComment-678248 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.