Solved

PHP Syntax

Posted on 2009-05-04
3
158 Views
Last Modified: 2013-12-12
Why isn't this working?  It always evaluates as incorrect, even if it's correct.
<?php

function curPageURL() {

 $pageURL = 'http';

 if ($_SERVER["HTTPS"] == "on") {$pageURL .= "s";}

 $pageURL .= "://";

 if ($_SERVER["SERVER_PORT"] != "80") {

  $pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];

 } else {

  $pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];

 }

 return $pageURL;

}

 

echo curPageURL();

echo ' (pulled url)<br>';

 

$currentURL = curPageURL();

 

echo $currentURL;

echo ' (variable url)';

 

if ( $currentURL == 'http://domain.net/members/$username/mystuff/stuff-home' ) {

$dynamicClass = 'selected';

echo '<br>correct';

} else {

$dynamicClass = 'NOTselected';

echo '<br>incorrect';

}

 

?>

Open in new window

0
Comment
Question by:bschwarting
  • 2
3 Comments
 
LVL 1

Author Comment

by:bschwarting
ID: 24298313
It looks like variable in the == is causing the issue.  When I hard code in the variable, it works fine.  Why isn't it validating properly with the variable $username in it?
0
 
LVL 14

Accepted Solution

by:
EMB01 earned 500 total points
ID: 24298340
I think it's because you need to use double-quotes, like so. Whenever you use single quotes, you can't use dynamic variables.

Ref. http://us.php.net/variables#39194
<?php

function curPageURL() {

 $pageURL = 'http';

 if ($_SERVER["HTTPS"] == "on") {$pageURL .= "s";}

 $pageURL .= "://";

 if ($_SERVER["SERVER_PORT"] != "80") {

  $pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];

 } else {

  $pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];

 }

 return $pageURL;

}

 

echo curPageURL();

echo ' (pulled url)<br>';

 

$currentURL = curPageURL();

 

echo $currentURL;

echo ' (variable url)';

 

if ( $currentURL == "http://domain.net/members/$username/mystuff/stuff-home" ) {

$dynamicClass = 'selected';

echo '<br>correct';

} else {

$dynamicClass = 'NOTselected';

echo '<br>incorrect';

}

 

?>

Open in new window

0
 
LVL 1

Author Comment

by:bschwarting
ID: 24298355
genius!  it's always the small things.  thanks!
0

Featured Post

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Creating and Managing Databases with phpMyAdmin in cPanel.
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to count occurrences of each item in an array.

862 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now