Page 1 of 1

img verification script - session problem in IE

Posted: Mon Jul 03, 2006 6:59 am
by kiq
Hi, I've just written an image verifcation script in PHP for a site I'm building, it generates a random 6 digit number, stores this in a session variable, creates an image from the string and is then called to the form page by 'img src=verif.php'. For some reason when using IE the random number in the session variable changes every few seconds so that by the time the form has been submitted the number that was entered doesn't match the session var. No prob in firefox, netscape or opera. The code is as follows:

Code: Select all

<?php
session_start(); 
srand(microtime() * 1000000); 
$secur = rand(100000,999999); 
$_SESSION['secur']=$secur; 
$newImg = imagecreate(135,40); 
$bg = imagecolorallocate($newImg,255,255,255); 
$textcolor = imagecolorallocate($newImg,rand(0,100), rand(0,100), rand(0,255)); 
$bgnoise=1; 
while ($bgnoise<15) 
{ 
$a=rand(0,135); 
$b=rand(0,40); 
$c=rand(0,135); 
$d=rand(0,40); 
$lincol1=rand(160,255); 
$lincol2=rand(215,255); 
$lincol3=rand(190,255); 
$linecolor=imagecolorallocate($newImg,$lincol1,$lincol2,$lincol3); 
imagefilledrectangle($newImg,$c,$d,$a,$b,$linecolor); 
$bgnoise++; 
} 
$font = imageloadfont("v3.gdf"); 
imagestring($newImg, $font, 10, rand(5,15), substr($secur,0,3) , $textcolor); 
imagestring($newImg, $font, 70, rand(5,15), substr($secur,3,6) , $textcolor); 
imagefill($newImg,0,0,$bg); 
header ("Content-type: image/gif"); 
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); 
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT"); 
header("Cache-Control: no-store, no-cache, must-revalidate"); 
header("Cache-Control: post-check=0, pre-check=0", false); 
header("Pragma: no-cache"); 
$newImg = imagerotate($newImg, rand(-10,10), 0); 
imagegif($newImg); 
imagedestroy($newImg); 

?>
I'm really tearing my hair out over this so any help greatly appreciated.

Carlos

Posted: Mon Jul 03, 2006 8:19 pm
by kiq
A slight correction to my above post:
The session variable is not changing every few seconds it appears to change once a few seconds after being generated the first time. Again, I am very confused :? about this, why would the number get regenerated after the script has been parsed? Doesn't seem to make sense (to me anyway).

The way I have checked to see when and how often the variable changes is by opening the following 2 pages on seperate tabs at the same:

Call the image script

Code: Select all

<html>
<head>
</head>
<body>
<img src="verif.php" alt="Copy this number below">
</body>
</html>
Show value of var every few secs

Code: Select all

<html>
<head>
<meta http-equiv="refresh" content="5">
</head>
<body>

<?php 

session_start();

echo $_SESSION['secur'];

?> 

</body>
</html>

Posted: Mon Jul 03, 2006 8:35 pm
by Christopher
I'm not sure what the problem is, but I do know that to get things like upload/download to work correctly in IE -- you need to add the following before session_start():

Code: Select all

session_cache_limiter("must-revalidate");

Posted: Tue Jul 04, 2006 6:14 am
by kiq
I'm thinking maybe this is a bug in IE7 beta which is what I have installed on my system, I just tried on a different pc with IE6 and everything worked fine! Interestingly, I experience the same problem on my system when using Netscape or FF (with the plugin) in IE mode! Does this mean that these 2 browsers use whatever IE engine you have installed on your system and don't have it built into them? I guess it must.

Arborint, thanks for your input, doesn't seem to have made any difference but I appreciate it and this was a function that I was unaware of and will bear in mind for the future.

Carlos