Solved

PHP Syntax

Posted on 2009-05-04
3
156 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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Suggested Solutions

Foreword In the years since this article was written, numerous hacking attacks have targeted password-protected web sites.  The storage of client passwords has become a subject of much discussion, some of it useful and some of it misguided.  Of cou…
Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

746 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

11 Experts available now in Live!

Get 1:1 Help Now