Jump to content

PHP code inside an echo?


ionicle
Go to solution Solved by PaulRyan,

Recommended Posts

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 by ionicle
Link to comment
Share on other sites

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 by HDRebel88
Link to comment
Share on other sites

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:

 

 

<?php

include "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 by ionicle
Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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 by ionicle
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.