1

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 />";
3
  • Parse error: syntax error, unexpected T_CONSTANT_ENCAPSED_STRING in /var/www/vhosts/liquor.com/subdomains/dev/httpdocs/wp-content/plugins/contact-form-7/includes/classes.php on line 348 Commented May 10, 2010 at 12:41
  • It has to be where the IP area is. When I put in the URL/variable combo I'm not doing it correctly... Commented May 10, 2010 at 12:41
  • It seems like in the anchor tag (urbangiraffe), you have quotes in it that you should escape. "<br /><br /><strong>IP:</strong> <a href=\"urbangiraffe.com/map/?ip=\"" is correct. Commented May 10, 2010 at 13:20

2 Answers 2

3

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 />";
Sign up to request clarification or add additional context in comments.

4 Comments

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: 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 ' :-)
ty so much GURU. I will now forever remember URLS should use one quote in PHP syntax.
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.
1

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. :-)

1 Comment

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.