CK9 Posted December 13, 2009 Share Posted December 13, 2009 I've been working on making my site less cluttered in the directories and more secure lately. In an attempt to prevent bandwidth theft from other people using images, I decided to switch all my site images to a dynamic image file. I've written a small test code to iron out as many bugs as I can BEFORE I impliment it fully. Code: <?php if ($_SESSION['i'] != "yes"} { $im = "theft"; } else { $im = $_GET['i']; } switch($im) { case 'theft": $image = imageCreateFromJPG('/theft.jpg'); break; case 'logo': $image = imageCreateFromJPG('/logo.jpg'); break; } imageJPG($image); imageDestroy($image); ?> When using it in the test copy of my site's index page (calling for the logo image) it works fine. When I try opening the page (with the GET['i'] set to logo, theft, and not set at all), I get the following error: Parse error: syntax error, unexpected T_IF in /home/ck9/public_html/media/images/site.php on line 3 I tried setting the current If...else as a long comment and re-writting it another way, but that didn't fix the issue. I tried looking up the error in the PHP manual, but the information presented wasn't helpful to me. Thanks in advance for any help you guys can provide (and the help here has always been great ) Quote Link to comment Share on other sites More sharing options...
teamatomic Posted December 13, 2009 Share Posted December 13, 2009 Curly brackets and round brackets are not compatible! if ($_SESSION['i'] != "yes"} Quote Link to comment Share on other sites More sharing options...
PFMaBiSmAd Posted December 13, 2009 Share Posted December 13, 2009 Don't use the GD functions to simply read and output an image. That will take up a huge amount of unnecessary server resources. You also need to output the correct content-type header before you output the image data. To do what you are trying (use a php script to output your images that are stored in a location that will not accept direct http requests) all you need to do is output the correct content-type header, then read and output the actual image file. The readfile function is normally used to read and output a file. Quote Link to comment Share on other sites More sharing options...
CK9 Posted December 13, 2009 Author Share Posted December 13, 2009 *facepalm* Why do I keep missing these small errors? Thanks atomic, but that doesn't clear the error (and before someone comes along and mentions it, I did use session_start, but I forgot to type it in my original post, heh) PFM, not quite sure what you're saying. Most of what I know of PHP coding I've learned through reading the tutorials and experimenting with the functions they give me...this readfile() wasn't in the ones I read. Do you have a link that will explain it as well as showing an example of its use? Quote Link to comment Share on other sites More sharing options...
Alex Posted December 13, 2009 Share Posted December 13, 2009 When in doubt check the manual Quote Link to comment Share on other sites More sharing options...
CK9 Posted December 13, 2009 Author Share Posted December 13, 2009 From what I read, you can use that to: force a download tell how many lines there are and what the last one is I don't see how that can be used to prevent other people from linking my site content to their site...Is there an example of it being used in this manner that I can look at and see if I can understand it better? Quote Link to comment Share on other sites More sharing options...
PFMaBiSmAd Posted December 14, 2009 Share Posted December 14, 2009 Programming requires a great amount of inference ( http://en.wikipedia.org/wiki/Inference ). Here is the code from the readfile() example, with only those things which I wrote above that are needed (content-type header and readfile() function) - <?php $file = 'your_file.jpg'; if (file_exists($file)) { header('Content-Type: application/octet-stream'); readfile($file); } ?> Here is the content-type header that matches your file type from the imagecreatefromjpeg/imagejpeg example (what your existing code seems to be based on) in the php manual - header('Content-Type: image/jpeg'); Inferred code - <?php $file = 'your_file.jpg'; if (file_exists($file)) { header('Content-Type: image/jpeg'); readfile($file); } ?> You would of course need to add the logic to get the proper file name and path to where the files are actually located into the $file variable. Quote Link to comment Share on other sites More sharing options...
CK9 Posted December 14, 2009 Author Share Posted December 14, 2009 I think I understand now. Just to confirm: 1) it looks at what the file is 2) it makes sure the file is at "localhost" 3) if it is, it displays the file. Is that the gist of it? Does this go in the coding for the page that has the image on it, or another file like a dynamic image is? Sorry for how difficult it is to explain this to me, because I've learned mostly through experimenting with the coding I don't always understand the terminology associated with it. Quote Link to comment Share on other sites More sharing options...
oni-kun Posted December 14, 2009 Share Posted December 14, 2009 I think I understand now. Just to confirm: 1) it looks at what the file is 2) it makes sure the file is at "localhost" 3) if it is, it displays the file. Is that the gist of it? Does this go in the coding for the page that has the image on it, or another file like a dynamic image is? Sorry for how difficult it is to explain this to me, because I've learned mostly through experimenting with the coding I don't always understand the terminology associated with it. if ($_SERVER['HTTP_HOST'] == "mysite.com") { $image = readfile('./image.jpg'); header('Content-type: image/jpeg'); imagejpeg($image); imagedestroy($image); //free resources } else { $image = readfile('./theft.jpg'); header('Content-type: image/jpeg'); imagejpeg($image); imagedestroy($image); } Like this? Very simple method, will only echo the actual image if the host is your own. EDIT: Fixed code a bit Quote Link to comment Share on other sites More sharing options...
CK9 Posted December 18, 2009 Author Share Posted December 18, 2009 Sorry for the delay, was preparing for the GRE (got 1230 on it ) Thanks for the example, I think that's what I was trying to do (only a much better method). As always, you guys here are great help Quote Link to comment 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.