mmendo Posted August 16, 2008 Share Posted August 16, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/120006-url-if-statement-not-working-w-variables/ Share on other sites More sharing options...
DeanWhitehouse Posted August 16, 2008 Share Posted August 16, 2008 i think you will need to use a $_GET statement for the extra vars Quote Link to comment https://forums.phpfreaks.com/topic/120006-url-if-statement-not-working-w-variables/#findComment-618171 Share on other sites More sharing options...
Fadion Posted August 16, 2008 Share Posted August 16, 2008 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']; ?> Quote Link to comment https://forums.phpfreaks.com/topic/120006-url-if-statement-not-working-w-variables/#findComment-618179 Share on other sites More sharing options...
php_dave Posted August 16, 2008 Share Posted August 16, 2008 $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 Quote Link to comment https://forums.phpfreaks.com/topic/120006-url-if-statement-not-working-w-variables/#findComment-618181 Share on other sites More sharing options...
Fadion Posted August 16, 2008 Share Posted August 16, 2008 @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. Quote Link to comment https://forums.phpfreaks.com/topic/120006-url-if-statement-not-working-w-variables/#findComment-618182 Share on other sites More sharing options...
DeanWhitehouse Posted August 16, 2008 Share Posted August 16, 2008 if ure right, shouldnt he use .htacess mod rewrite? Quote Link to comment https://forums.phpfreaks.com/topic/120006-url-if-statement-not-working-w-variables/#findComment-618186 Share on other sites More sharing options...
Fadion Posted August 16, 2008 Share Posted August 16, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/120006-url-if-statement-not-working-w-variables/#findComment-618187 Share on other sites More sharing options...
mmendo Posted August 16, 2008 Author Share Posted August 16, 2008 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 . . . Quote Link to comment https://forums.phpfreaks.com/topic/120006-url-if-statement-not-working-w-variables/#findComment-618188 Share on other sites More sharing options...
Fadion Posted August 16, 2008 Share Posted August 16, 2008 mmendo, did you see the second post of this topic? <?php $page = $_SERVER['PHP_SELF']; ?> Quote Link to comment https://forums.phpfreaks.com/topic/120006-url-if-statement-not-working-w-variables/#findComment-618189 Share on other sites More sharing options...
DeanWhitehouse Posted August 16, 2008 Share Posted August 16, 2008 can i ask, is the statement correct, does the URI equal the same? Quote Link to comment https://forums.phpfreaks.com/topic/120006-url-if-statement-not-working-w-variables/#findComment-618190 Share on other sites More sharing options...
mmendo Posted August 16, 2008 Author Share Posted August 16, 2008 Blade280891, Yes the URI equals the same which is why I am so confused. GuiltyGear, PHP_SELF does not output the variables, so it would work for pages with no variables, but not on pages with variables. Quote Link to comment https://forums.phpfreaks.com/topic/120006-url-if-statement-not-working-w-variables/#findComment-618193 Share on other sites More sharing options...
mmendo Posted August 16, 2008 Author Share Posted August 16, 2008 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')"; } Quote Link to comment https://forums.phpfreaks.com/topic/120006-url-if-statement-not-working-w-variables/#findComment-618196 Share on other sites More sharing options...
Fadion Posted August 16, 2008 Share Posted August 16, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/120006-url-if-statement-not-working-w-variables/#findComment-618198 Share on other sites More sharing options...
DeanWhitehouse Posted August 16, 2008 Share Posted August 16, 2008 echo the URI and compare it Quote Link to comment https://forums.phpfreaks.com/topic/120006-url-if-statement-not-working-w-variables/#findComment-618200 Share on other sites More sharing options...
php_dave Posted August 16, 2008 Share Posted August 16, 2008 @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. Quote Link to comment https://forums.phpfreaks.com/topic/120006-url-if-statement-not-working-w-variables/#findComment-618203 Share on other sites More sharing options...
mmendo Posted August 16, 2008 Author Share Posted August 16, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/120006-url-if-statement-not-working-w-variables/#findComment-618207 Share on other sites More sharing options...
DeanWhitehouse Posted August 16, 2008 Share Posted August 16, 2008 are you checking for the whole URI, or just if the vars are there? Quote Link to comment https://forums.phpfreaks.com/topic/120006-url-if-statement-not-working-w-variables/#findComment-618208 Share on other sites More sharing options...
Fadion Posted August 16, 2008 Share Posted August 16, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/120006-url-if-statement-not-working-w-variables/#findComment-618209 Share on other sites More sharing options...
DeanWhitehouse Posted August 16, 2008 Share Posted August 16, 2008 so we go back to what i said in the first place. why do i bother posting ??? Quote Link to comment https://forums.phpfreaks.com/topic/120006-url-if-statement-not-working-w-variables/#findComment-618210 Share on other sites More sharing options...
Fadion Posted August 16, 2008 Share Posted August 16, 2008 lol Blade. Actually the question seemed very different in the beginning. Anyway you got it Quote Link to comment https://forums.phpfreaks.com/topic/120006-url-if-statement-not-working-w-variables/#findComment-618220 Share on other sites More sharing options...
DeanWhitehouse Posted August 16, 2008 Share Posted August 16, 2008 Anyway you got it Hopefully Quote Link to comment https://forums.phpfreaks.com/topic/120006-url-if-statement-not-working-w-variables/#findComment-618221 Share on other sites More sharing options...
mmendo Posted August 17, 2008 Author Share Posted August 17, 2008 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 . . . Quote Link to comment https://forums.phpfreaks.com/topic/120006-url-if-statement-not-working-w-variables/#findComment-618273 Share on other sites More sharing options...
Fadion Posted August 17, 2008 Share Posted August 17, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/120006-url-if-statement-not-working-w-variables/#findComment-618280 Share on other sites More sharing options...
mmendo Posted August 17, 2008 Author Share Posted August 17, 2008 Thanks. That did the trick. I appreciate your help & your patience. Quote Link to comment https://forums.phpfreaks.com/topic/120006-url-if-statement-not-working-w-variables/#findComment-618299 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.