tags:

views:

38

answers:

2

I am trying to pass a variable (in this case an IP variable of the user) into a url so when it is displayed on the web it is an automatic link. Below is the code I have and I'm getting an error. Seeking PHP Guru to help a n00b.

$user_tracking_vars = "<br /><br /><strong>Browser and Operating System:</strong> ".$browser."<br /><br /><strong>IP:</strong> <a href=""http://urbangiraffe.com/map/?ip=".$ip."&amp;from=drainhole""&gt;".$ip."&lt;/a&gt;&lt;br /><br /><strong>Page Visited Before Contact Form:</strong> ".$referred."<br />";
+2  A: 

It looks like you're incorrectly escaping your quotes using Basic-like (href=""..."") syntax. The escape character in PHP is backslash (href=\"...\").

$user_tracking_vars = "<br /><br /><strong>Browser and Operating System:</strong> ".$browser."<br /><br /><strong>IP:</strong> <a href=\"http://urbangiraffe.com/map/?ip=".$ip."&amp;from=drainhole"\&gt;".$ip."&lt;/a&gt;&lt;br /><br /><strong>Page Visited Before Contact Form:</strong> ".$referred."<br />";

You can also alternate the quotes used to achieve the same effect (href='...'):

$user_tracking_vars = "<br /><br /><strong>Browser and Operating System:</strong> ".$browser."<br /><br /><strong>IP:</strong> <a href='http://urbangiraffe.com/map/?ip=".$ip."&amp;from=drainhole'&gt;".$ip."&lt;/a&gt;&lt;br /><br /><strong>Page Visited Before Contact Form:</strong> ".$referred."<br />";
Andy E
+1 nice catch..
zaf
You can also alternate the quote between the href (defined with ") and the string definition (defined with '). String will not be parsed for variables (useless since variables are assembled using a dot), it is much more efficient.
snowflake
@snowflake: not sure how much *more efficient* it is, but I was just being lazy -- it was easier to change the quotes around the `href` value than to hunt for the closest string delimiters and change those to `'` :-)
Andy E
ty so much GURU. I will now forever remember URLS should use one quote in PHP syntax.
HollerTrain
Ah beat me to it. Also, might I add that in future cases, you should use single quotes. You can put in like '"hello world"' and it would output. It's also a bit faster because with double quotes, it also attempts to parse out variable substitutions, but with single quotes, it will just output the raw text without substitutions.
Daniel
+1  A: 

If you don't want trouble escaping long strings of html you should try doing this:

$ip = "...";
$browser = "...";
$referred = "...";

$user_tracking_vars =<<<text
    <br/>
    <br/>
    <strong>Browser and Operating System:</strong>
    $browser
    <br/><br/>
    <strong>IP:</strong>
    <a href="http://urbangiraffe.com/map/?ip={$ip}&amp;from=drainhole"&gt;$ip&lt;/a&gt;
    <br/><br/>
    <strong>Page Visited Before Contact Form:</strong>
    $referred
    <br/>
text;
// remember the text; from line above must start @ char 0...

or this:

<?php 
$ip = "...";
$browser = "...";
$referred = "...";
?>

<br/>
<br/>
<strong>Browser and Operating System:</strong>
<?php echo $browser; ?>
<br/><br/>
<strong>IP:</strong>
<a href="http://urbangiraffe.com/map/?ip=&lt;?php echo $ip;?>&from=drainhole"><?php echo $ip;?></a>
<br/><br/>

<strong>Page Visited Before Contact Form:</strong>
<?php echo $referred; ?>
<br/>

Any of the above will save you precious time escaping quotes. Since I don't know in which context you're using $user_tracking_vars there's no need to discuss the advantages of having logic and output apart. :-)

acmatos
+1, the second is a great example of using PHP in it's "templating" style.
Andy E