Jump to content

Separating two variables in an image string


Go to solution Solved by benanamen,

Recommended Posts

HI There,

 

I am trying to separate two variables in a image string my code looks like this

ECHO "<td width='100'><div align='center'><img src=\"http://www.brickbybricks.ca/php/images/".$row['Color_Name'].$row['PartID'].".gif\"></td>";

I cannot seem to figure out what to put between the two variables to make the img src work, the way it is right now I get the link back looking like this.

 

 

<img src="http://www.brickbybricks.ca/php/images/Black4733.gif">

 

So I need a slash in between black at 4733

 

Thanks,

You didn't escape the " at the end of the src= string.  Try using single quotes so you don't have to escape the doubles.

echo "<td width='100'><div align='center'><img src='http://www.brickbybricks.ca/php/images/" . $row['Color_Name'].$row['PartID'] . ".gif'></td>";

 

See how the entire string is in double quotes but the attributes inside of the tags are in single ones?

  • Solution
echo "<td width=\"100\"><div align=\"center\"><img src=\"http://www.brickbybricks.ca/php/images/{$row['Color_Name']}/{$row['PartID']}.gif\"></td>";

See how the entire string is in double quotes but the attributes inside of the tags are in single ones?

 The only thing about that is you have no choice but to escape and concatenate the variables instead of doing {$row['Color_Name']} (Curly Syntax, my preference). 

Edited by benanamen

Dumping raw variables into HTML markup is insecure and can lead to very nasty bugs. For example, if your color happens to contain a special character, you'll quickly run into syntax conflicts. And if an attacker is able to control the variables (e. g. by manipulating your database), this code can be used to perform a cross-site scripting attack.

 

Always URL-encode and HTML-escape variables before inserting them into links:

<?php

function html_escape($raw_input, $encoding)
{
    return htmlspecialchars($raw_input, ENT_QUOTES | ENT_SUBSTITUTE | ENT_HTML5, $encoding);
}



$image_url = 'http://www.brickbybricks.ca/php/images/'.rawurlencode($row['Color_Name']).'/'.rawurlencode($row['PartID']).'.gif';

echo '<td width="100"><div align="center"><img src="'.html_escape($image_url, 'UTF-8').'"></td>';

Note that using style attributes like width or align is heavily outdated. You should be using CSS. It's also bad practice to mix HTML markup into PHP code. Keep them separate:

<?php

// Your PHP code goes here

?>
<!--

Your HTML markup goes here

-->
Edited by Jacques1
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.