ionicle Posted July 21, 2013 Share Posted July 21, 2013 (edited) I'm having a conditional echo inside my php code, that outputs certain HTML if the condition is met. echo" <html-here> Everything's great, I'm escaping all double quotes in the html itself, so that doesn't interfere with its display, except for one line: <img id='cpt_img' src='<?php echo $path . $img ?>'> The line contains a captcha image randomizer that is echoed in PHP. How do I make this work, inside the already existing "echo"? Edited July 21, 2013 by ionicle Quote Link to comment Share on other sites More sharing options...
HDRebel88 Posted July 21, 2013 Share Posted July 21, 2013 (edited) If you're wrapping your html with double quotes all you have to do is use the variable inside the double quotes: echo "<td valign='top' width='123'><font face='Arial, Helvetica, sans-serif' size='2'><img id='cpt_img' src='$path.$img'>" If you're using single quotes with the echo (which it seems like you are not) then you have to escape variables, by concatenating the variables to the echo: echo '<td valign="top" width="123"><font face="Arial, Helvetica, sans-serif" size="2"><img id="cpt_img" src="'.$path.$img.'">' Also just as a note of HTML code, you should close or self close img tags. <img id="cpt_img" src=""></img> or <img id="cpt_img" src="" /> Edited July 21, 2013 by HDRebel88 Quote Link to comment Share on other sites More sharing options...
ionicle Posted July 21, 2013 Author Share Posted July 21, 2013 (edited) For some weird reason, it's still not working. echo "<td valign='top' width='123'><font face='Arial, Helvetica, sans-serif' size='2'><img id='cpt_img' src='$path.$img'>" I have included the relevant php code for the randomizer in the beginning of the php code itself. What am I missing? I tried running it all as a separate file: <?phpinclude "turing.php";?> and the HTML code below. It works if the "src" field is "src='<?php echo $path . $img ?>'", but when I change it to "src='$path . $img' " - no luck. Edited July 21, 2013 by ionicle Quote Link to comment Share on other sites More sharing options...
HDRebel88 Posted July 21, 2013 Share Posted July 21, 2013 Put error_reporting(E_ALL); right after the PHP opening tag at the top of the file. That'll generate notices of any errors there are on the page. Reload the page and see what they say. Quote Link to comment Share on other sites More sharing options...
ionicle Posted July 21, 2013 Author Share Posted July 21, 2013 No errors are reported at all. Here's how it goes: <?php error_reporting(E_ALL); include "turing.php"; echo "html-code-here-all-the-way-up-to <img id='cpt_img' src='$path . $img'> some-more-html"; ?> The randomizer code, contained within "turing.php" is: <?php function getImagesFromDir($path) { $images = array(); if ( $img_dir = @opendir($path) ) { while ( false !== ($img_file = readdir($img_dir)) ) { // checks for gif, jpg, png if ( preg_match("/(\.gif|\.jpg|\.png)$/", $img_file) ) { $images[] = $img_file; } } closedir($img_dir); } return $images; } function getRandomFromArray($ar) { mt_srand( (double)microtime() * 1000000 ); // php 4.2+ not needed $num = array_rand($ar); return $ar[$num]; } ///////////////////////////////////////////////////////////////////// // This is the only portion of the code you may need to change. // Indicate the location of your images $root = ''; // use if specifying path from root //$root = $_SERVER['DOCUMENT_ROOT']; $path = 'elements_/captcha/'; // End of user modified section ///////////////////////////////////////////////////////////////////// // Obtain list of images from directory $imgList = getImagesFromDir($root . $path); $img = getRandomFromArray($imgList); ?> Again, it works fine if the source field has an internal php echo the two variables $path and $img, and the whole html is not placed within an echo. When I change it to src='$path . $img', it simply breaks, as if it cannot find the path to the image directory. Quote Link to comment Share on other sites More sharing options...
ionicle Posted July 21, 2013 Author Share Posted July 21, 2013 (edited) If I echo it with src='<?php echo $path . $img ?>, I get the following entry in my server log: domain.name - - [21/Jul/2013:15:04:34 +0300] "GET /%3C?php%20echo%20elements_/captcha/%20.%20ePa92ZmF2SZRboS.jpg%20?%3E HTTP/1.1" 500 415 It just treats the "<?php ?> bit as a part of the location of the images. How do I prevent that? Edited July 21, 2013 by ionicle Quote Link to comment Share on other sites More sharing options...
Solution PaulRyan Posted July 21, 2013 Solution Share Posted July 21, 2013 You do not need the dot between the 2 variables, as this adds an actual dot between the 2 values. Use this instead: <img id='cpt_img' src='{$path}{$img}'> Quote Link to comment Share on other sites More sharing options...
ionicle Posted July 21, 2013 Author Share Posted July 21, 2013 Yup, that did the trick! Thank you VERY MUCH! 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.