webref.eu Posted June 22, 2011 Share Posted June 22, 2011 Hi Guys It's been a while since I've done any PHP coding, and I need a simple script to display the large version of an image, so for example, pass the script the following url: photo.php?photo=133 I need the script to: - get the image ref, i.e. 133, so I can reference a large version of the image stored in the images/large/ folder. - cleanse the variable, i.e. protect against any hacking attempt I'm looking in to how to do this at the moment as I'm a bit out of practice, but as I understand this is basic stuff, if any of the pros can tell me the code, that would be much appreciated as it will save me some valuable time. Cheers Quote Link to comment https://forums.phpfreaks.com/topic/240139-help-with-a-simple-image-viewer-script/ Share on other sites More sharing options...
fugix Posted June 22, 2011 Share Posted June 22, 2011 On your photo.php page, you will want to grab the query string variable using $_GET['photo'] The only "cleansing" of this variable that really needs done is to make sure that the variable passed through the URL is a numeric value. $photo_id = $_GET['photo']; if (is_numeric($photo_id)) { // do something with the photo_id } else { trigger_error('invalid photo id',E_USER_ERROR); } Quote Link to comment https://forums.phpfreaks.com/topic/240139-help-with-a-simple-image-viewer-script/#findComment-1233476 Share on other sites More sharing options...
TeNDoLLA Posted June 22, 2011 Share Posted June 22, 2011 If you have the image id as auto_increment in database it will be unsigned int, in which case it is better to check against if (intval($_GET['photo']) > 0) { // image id is valid } ..since is_numeric can take some other values also than normal numbers like +0123.45e6 or hexadecimal notations. Quote Link to comment https://forums.phpfreaks.com/topic/240139-help-with-a-simple-image-viewer-script/#findComment-1233484 Share on other sites More sharing options...
webref.eu Posted June 22, 2011 Author Share Posted June 22, 2011 fugix - Many thanks for your help, that's great. TeNDoLLA - Thanks for your comments. Rgds Quote Link to comment https://forums.phpfreaks.com/topic/240139-help-with-a-simple-image-viewer-script/#findComment-1233488 Share on other sites More sharing options...
fugix Posted June 22, 2011 Share Posted June 22, 2011 If you have the image id as auto_increment in database it will be unsigned int, in which case it is better to check against if (intval($_GET['photo']) > 0) { // image id is valid } ..since is_numeric can take some other values also than normal numbers like +0123.45e6 or hexadecimal notations. True, however intval gives unexpected results with integers. It's more designed for strings Quote Link to comment https://forums.phpfreaks.com/topic/240139-help-with-a-simple-image-viewer-script/#findComment-1233495 Share on other sites More sharing options...
Pikachu2000 Posted June 22, 2011 Share Posted June 22, 2011 Like all form data, $_GET['photo'] is a string. The best way to validate that value would be with ctype_digit IMO, and cast it as an integer for use in the query string. if( ctype_digit(trim($_GET['photo'])) ) { $photo_id = (int) $_GET['photo']; $query = "SELECT field FROM table WHERE photo_id = $photo_id"; } else { // possible attempt to pass an invalid value in URL } Quote Link to comment https://forums.phpfreaks.com/topic/240139-help-with-a-simple-image-viewer-script/#findComment-1233505 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.