img src on one page calls php script to dump 5 random src's from a possible 15.

Please Help Me!

I’m not very proficient in php yet.

So I have five of these lined up, just one for now, I haven’t figured out how to loop through and dump 5 yet.

<img src="/getVerifyPics.php5" id="" class="humanPics" alt="" onclick="" />

But getVerifyPics.php5 isn’t working. When I run it through this online php checker thing (http://www.icosaedro.it/phplint/phplint-on-line.html) it hits an error in the second for loop. Something about expecting a string when it’s a mixed expression. And I think I need to change the root directory somehow? I tried to make it clear ish with comments.

[php]<?php
//names of the 5 images
$letter = array(“A”, “B”, “C”, “D”, “E”); //first half
shuffle($letter);

$number = array(); //second half
for ($i=0; $i<=4; $i++) {
	$number[$i] = rand(1,3);//5 random numbers 1-3 stored in num0 to num4
}
shuffle($number);

//put the pieces together; randomly ordered picture files
$img = array();
for ($pi=0; $pi<=4; $pi++) {
	$img[$pi] = "/VerifyPics/" . $letter[$pi] . $number[$pi] . ".jpg";
}
shuffle($img); //one last shuffle for good measure

$fp = fopen($img[0], 'rb');

//headers
header("Content-Type: image/jpeg");
header("Content-Length: " . filesize($img[0]));

//dump and exit
fpassthru($fp);
exit;

?>[/php]

That code is fine, ignore what that site said - it says some ridiculous things after the tests I’ve just run through it.

It’s most likely that your problem lies in your image paths. Have you tried opening that PHP file in your browser first just to check that it’s outputting properly?

Also try using relative paths for your images - the / is no doubt correct for HTML, but PHP can get completely different results when looking for the folder “/”. Depending on your folder structure you might need to make it “…/VerifyPics/” or even “…/…/VerifyPics/” (etc).

Wouldn’t it be easier to just grab the list of filenames for the pictures using the “dir” command and then just randomly pick your 5 from the list. Do you really need all that code for alpha’s and numbers?

Something like:
$filenames = scandir($dir);
$imagename = $filenames(rand(2,count($filenames)));

You can not use the first two names as they are “.” and “…” so randomly use 2 to max names…

Might make it easier…

Ha! this has been bugging me for a few days now and all I had to do was take out the / before the directory. thanks Smokey

OK. So I’ve done a bit of honing and toning, and am having a different problem now. There are five images on my html page like so:

<img src="/getVerifyPics.php5?HVid=0&HVlet=<?php echo $letters; ?>" id="HV0" class="humanPics" alt="" onclick="" /><img src="/getVerifyPics.php5?HVid=1&HVlet=<?php echo $letters; ?>" id="HV1" class="humanPics" alt="" onclick="" /><img src="/getVerifyPics.php5?HVid=2&HVlet=<?php echo $letters; ?>" id="HV2" class="humanPics" alt="" onclick="" /><img src="/getVerifyPics.php5?HVid=3&HVlet=<?php echo $letters; ?>" id="HV3" class="humanPics" alt="" onclick="" /><img src="/getVerifyPics.php5?HVid=4&HVlet=<?php echo $letters; ?>" id="HV4" class="humanPics" alt="" onclick="" />

And my new getVerifyPics.php5 looks like this:

[php]<?php
$theID = $_REQUEST[“HVid”];
if (isset($theID) && $theID != NULL && $theID != “”) {
$id = $theID;
}
else {
echo “HV image id is not set; behave.”;
exit;
}

//prepare names
$letters = unserialize($_REQUEST["HVlet"]); //first half

//if $letter is shuffled every time there could be more than one occurrence
if (!isset($letters) || $letters == NULL || $letters == "") {
	$letter = array("A", "B", "C", "D", "E");
	shuffle($letter);
}		

$number = rand(1,3); //second half

//put the pieces together
$img = "VerifyPics/" . $letter[$id] . $number . ".jpg";

$fp = fopen($img, 'rb');

//headers
header("Content-Type: image/jpeg");
header("Content-Length: " . filesize($img));

//dump, pass shuffled letter array and exit
fpassthru($fp);
$letters = $_POST[serialize($letter)];
exit;

?>[/php]

Something is wrong with my $letters array. It’s not passing it or something because it’s hitting shuffle($letter) everytime the page is called. Which results in some letters being used more than once, and some not being used at all. I need to create the $letter array once, and send it to all the other images so that I have 5 images, A B C D and E, randomly ordered. Can anyone see what is wrong?

Sponsor our Newsletter | Privacy Policy | Terms of Service