Zach Smith
Zach Smith

Reputation: 5674

Placing PHP Variable in URL

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."&from=drainhole"">".$ip."</a><br /><br /><strong>Page Visited Before Contact Form:</strong> ".$referred."<br />";

Upvotes: 1

Views: 9681

Answers (2)

acm
acm

Reputation: 6637

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}&from=drainhole">$ip</a>
    <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=<?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. :-)

Upvotes: 1

Andy E
Andy E

Reputation: 344517

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."&from=drainhole"\>".$ip."</a><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."&from=drainhole'>".$ip."</a><br /><br /><strong>Page Visited Before Contact Form:</strong> ".$referred."<br />";

Upvotes: 3

Related Questions