Page 1 of 1

A newbie question re php varibles inside an IF

Posted: Sun Jul 02, 2006 10:43 am
by rcgauer
Hello....

I can use php to determine if a url variable exists and echo something to the screen based upon it:

<?php
if ($_GET["ID"] != '')
{ echo "there is no ID";
}
else {
echo "yes there is";
}
?>

but I can't figure a bowlegs and curly braces syntax that allows me to do further php stuff based on that knowledge. I would like, for example, to include a specific hyperlink or image based on the URL variable. These work on their own:

<?php print '<a href="printer_friendly.mvc?ID=' . $_GET["ID"] . '">' ;?>printer friendly</a>
<?php print '<IMG SRC="images/' . $_GET["ID"] . '.gif" >';?>

How can I stuff that sort of thing inside the conditional statement, so that the image or link only appears if the url variable exists?

Thanks
rg

Posted: Sun Jul 02, 2006 1:03 pm
by John Cartwright

Code: Select all

<?php

if (!empty($_GET['ID']) && is_numeric($_GET['ID']))
{ 
    print '<a href="printer_friendly.mvc?ID=' . $_GET['ID'] . '">printer friendly</a>';
    print '<IMG SRC="images/' . $_GET['ID'] . '.gif" >';
}

?>
Have a look at the isset() and empty() functions when checking if variables exist and or are empty or not. You should always check whether the data you are expecting is the proper type. Have a search for "input sanitation" and "XSS injection"

Posted: Sun Jul 02, 2006 2:19 pm
by rcgauer
Thanks