PHP error: undefined index when requesting an id

So I am attempting to build and view a PHP script that displays one of two images based upon requesting id.

I get this error when trying to view the script.
Undefined index: id in /storage/ssd1/170/1940170/public_html/simple.php on line 6

I am using:

<?php header("Content-Type: image/png"); if ($_REQUEST['id']==1) { readfile("image.png"); } else if ($_REQUEST['id']==2) { readfile("image2.png");** } ?>

Am I using an incorrect syntax? Could this be a browser issue because at the moment I am running firefox and am unable to use a different computer to test a different browser. Is this an issue with this host and how it is set up? If any one could give some advice, that would be great! Thank you! ^^

Hi @echo-doodle!

Undefined index: id

This means $_REQUEST["id"] vector does not exist.

You can avoid this issue by adding an if statement with isset() function, to check if id parameters is specified in request.

<?php
header("Content-Type: image/png");

if(isset($_REQUEST['id']))
{
   if ($_REQUEST['id'] == 1) {
      readfile("image.png");
   } else if ($_REQUEST['id'] == 2) {
      readfile("image2.png");**
   }
}
else
{
   echo "No id provided";
}
?>

@NGiNX,

Thank you so much for responding! I updated the code and now it says no id provided which per the code, it is a correct response. Though this goes against my purpose of running the script. Please pardon my coding/technical ignorance, but how does one provide an id? Is it a fault of my browser that I am not providing an id or something else?

$_REQUEST is a super global variable which contains all other variables $_GET, $_POST, $_COOKIE.

Technically, you should either provide an id parameter through GET/POST method or a cookie named id when calling your PHP script which contains the given code.

I am not sure if this problem comes from browser. More like from buggy code…

@NGiNX,

Thank you for the information!

I have taken a look through the PHP manual on sessions and have some questions. From my understanding you can get a session id, but if you do not have a session id then you will need to start one. You can also set an id, but for my purposes of the script there needs to be either an id of 1 or 2. Is it possible then to set the id randomly between id 1 and 2 or through a calculated percent such as dole out id 1, 60% of the time and dole out id 2, 40% of the time?

I feel like this is possible through an if/then statement, but I have no clue what to check for to run it properly.

If you want your image to be read randomly, we’re complicating with sessions… You can use something like this instead:

<?php
header("Content-Type: image/png");

$nr = rand(0, 1);

if($nr == 0)
{
   readfile("image.png");
}
else
{
   readfile("image2.png");
}
?>

We generate either 0 or 1 randomly. According to the number, we read the image.

@NGiNX,

So I followed your advice and it worked!

I am following a larger tutorial which this was the start of. Going through the tutorial further I broke code along the way, but managed to fix everything except this: Fatal error: Cannot redeclare find_adoptable() (previously declared in /storage/ssd1/170/1940170/public_html/adopttools.php:5) in /storage/ssd1/170/1940170/public_html/adopttools.php on line 16

Would you mind taking a look please?

I thought it might be talking about this bit: $adoptable=find_adoptable($adoptid);
So I took it out and it still gave an error so now I am stumped because per what is says being line 16, it comes to a closing bracket not a line of code? I have been looking through code so much I may be code blind and completely missing something extremely obvious. XD If you could help that would be awesome!

Here is the code the error is referring too:

<?php $adoptxml=simplexml_load_file("adoptables.xml"); function find_adoptable($id) { global $adoptxml; foreach ($adoptxml->adoptable as $adoptable) { if ($adoptable['id']==$id) { return $adoptable; } } return NULL; } function find_variation($adoptid, $varid) { $adoptable=find_adoptable($adoptid); foreach ($adoptable->variation as $variation) { if ($variation['id']==$varid) { //Yes, it is return $variation; } } return NULL; } ?>

Fatal error: Cannot redeclare find_adoptable() (previously declared in /storage/ssd1/170/1940170/public_html/adopttools.php:5) in /storage/ssd1/170/1940170/public_html/adopttools.php on line 16

This means your function has already been declared earlier. Make sure your function name does not interfere with anything else in your code.

Another easy way to do this is:
 <?php
$rm = rand(0, 1);
if ($rm==0) {
$image = ("image.png");
} else if ($rm==1) {
$image = ("image2.png");
}
?>
<!DOCTYPE html>
<html>
<body>
<img src="<?php echo $image; ?>">
</body>
</html>

Maybe give it a try?