Jump to content

URL If Statement Not Working w/ Variables


mmendo

Recommended Posts

Hello,

 

I have code to get the url of the current page:

$page = $_SERVER['REQUEST_URI']; 

 

I have tested it with echo and it does properly show the url

 

I then have an if statement to expand a part of the page.  It works when there are not variables attached to the end of the url, but does not work when there are variables.

 

For example, this works if I am on the URL /admin/manufacturers.php:

if ($page == '/admin/manufacturers.php') { 
$manual = "MoreOrLess('span2')"; 
}

 

 

But this does NOT work when I am on the URL /admin/manufacturers.php?page=1&mID=6&action=new:

if ($page == "/admin/manufacturers.php?page=1&mID=6&action=new") { 
$manual = "MoreOrLess('span2')"; 
} 

 

As I mentioned, I did test and $page is returning the ENTIRE url (with variables, if they are present).  Any idea, then, why this does not work?

 

Thanks for any help.

Link to comment
Share on other sites

U can use 'PHP_SELF' to show just the url without the GET variables:

 

<?php
echo $_SERVER['PHP_SELF'];
?>

 

while this could have the same effect as 'REQUEST_URI':

<?php
echo $_SERVER['PHP_SELF'] . '?' . $_SERVER['QUERY_STRING'];
?>

 

Link to comment
Share on other sites

$page = $_SERVER['REQUEST_URI'];
$query = $_SERVER['QUERY_STRING'];
$fullString = $page.$query;

 

REQUEST_URI does not return the query string  (everything including the ? onwards) - you need to pull that from QUERY_STRING and add it on.

 

Hope this helps

 

Dave

Link to comment
Share on other sites

if ure right, shouldnt he use .htacess mod rewrite?

 

It should be the topic starter to respond, but anyway... He just needs to check if someone visits a certain page and if yes, make something. Don't see your point of htaccess. At least that's how I understood the problem.

Link to comment
Share on other sites

When I test by echoing $page where $page is

$page = $_SERVER['REQUEST_URI']; 

It DOES return the full string, including the extra vars.

 

However, it seems the if statement does not work but I can't for the life me figure out why . . .

Link to comment
Share on other sites

The first if statement I show (without variables) works:

if ($page == '/admin/manufacturers.php') { 
$manual = "MoreOrLess('span2')"; 
}

 

The second one (WITH variables) does NOT work.  Is there something wrong with the if statement itself since it seems the URI is working correctly? 

if ($page == "/admin/manufacturers.php?page=1&mID=6&action=new") { 
$manual = "MoreOrLess('span2')"; 
} 

Link to comment
Share on other sites

mmendo, are you trying the code people provide? PHP_SELF will get you the url without get variables, when those are present or not. I tested it to be sure and it works. The if statement you are providing doesn't make sense, in that case just use the get superglobal, but guess that's not what u want to achieve.

Link to comment
Share on other sites

@php_dave you're wrong. REQUEST_URI returns the full url with the get variables (query string). The topic starter wanted a way to hide the query string.

 

Aye it appends 'QUERY_STRING' - my bad - apologies for the bum steer.

 

As for the OPS comparison problem - I cant see any logic problems - I just replicated on my machine and the comparison returns True.

 

 

Link to comment
Share on other sites

Hi GuiltyGear,

 

If I am correct, I think you may misunderstand what I am trying to do.  It is important for me to be able to compare the URL with variables in my if statement.  I think I may have made this confusing by showing that they both output $manual = "MoreOrLess('span2')";

 

In actuality, the second statement (with variables) should expand 'span3'.  I am using this to expand a help menu to correct section based on the URL of the open web page.  In many cases, the base url (such as manufacturers.php) needs to expand a different section than the url with variables.

 

I have tried all that has been suggested, but I still don't seem to have it working even though 'REQUEST_URI' outputs the same string as the comparison string in my if statement.

 

Link to comment
Share on other sites

Ok now it makes sense. I was confused by the code snippets you gave. You can try something like this:

 

<?php
if(isset($_GET['page'])){
     //make something
} elseif(isset($_GET['mID'])){
    //make something else
} elseif($_GET'action'] == 'new'){
    //again make something else
}
?>

 

You can check if a get variable isset() or if it has a certain value. Hope this clears your problem out.

Link to comment
Share on other sites

I apologize as I am very much a beginner . . .  I am sorry too to, Blade280891, for not understading the GET vars . .

This is what I need to do.  Will this return any URL that had a ?page= in the url?  How I do I make it so it only returns those that first have the 'PHP_SELF' beginning?

 

Sorry if my terminoligy is wrong . . . I hope this makes sense . . .

 

Link to comment
Share on other sites

You can use just the get variables to check certain conditions. Lets say:

 

<?php
$page = $_GET['page']; //get the page variable
if(isset($page)){ //check if page is set, meaning the url is something like: index.php?page=var
     if($page == 'about'){ //check if the url is: index.php?page=about
          echo 'This is the about page';
     } elseif($page == 'contant'){ //check if the url is: index.php?page=contact
          echo 'Do you want to contact me?';
     }
} else{ //this will happen if the page isn't set, meaning you'll have only: index.php
     echo 'Still in homepage?';
}
?>

 

Guess that makes things clear on using get variables. Basically you check if a variable is set with isset() and also check if it meets a certain condition.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.