Cory94bailly Posted February 15, 2009 Share Posted February 15, 2009 Hi. My current code: $page = basename($_SERVER['PHP_SELF']); //Current page user is on I am using that am I am getting "index.php" and that is basically it.. But I would like it to include stuff like "index.php?page=links"... I tried every link I have on my site with the log and I just get "index.php"... I DO know of another way IF NEEDED... But this would be great if it could be improvised Link to comment https://forums.phpfreaks.com/topic/145269-solved-very-easy-help-getting-current-url/ Share on other sites More sharing options...
corbin Posted February 15, 2009 Share Posted February 15, 2009 Look at print_r($_SERVER). Link to comment https://forums.phpfreaks.com/topic/145269-solved-very-easy-help-getting-current-url/#findComment-762613 Share on other sites More sharing options...
Cory94bailly Posted February 15, 2009 Author Share Posted February 15, 2009 Look at print_r($_SERVER). Sorry, do you want me to try that out or to use it instead of what I had? Link to comment https://forums.phpfreaks.com/topic/145269-solved-very-easy-help-getting-current-url/#findComment-762620 Share on other sites More sharing options...
supposedlysupposed Posted February 15, 2009 Share Posted February 15, 2009 echo $_SERVER[php_SELF] ."?". $_SERVER[QUERY_STRING]; Should give you the entire thing. Link to comment https://forums.phpfreaks.com/topic/145269-solved-very-easy-help-getting-current-url/#findComment-762626 Share on other sites More sharing options...
supposedlysupposed Posted February 15, 2009 Share Posted February 15, 2009 Look at print_r($_SERVER). Sorry, do you want me to try that out or to use it instead of what I had? I just got it now, I think Corbin meant to look at the output of print_r($_SERVER), literally putting in on a page and seeing what it shows, because it has the things necessary to make the full url within its array. Link to comment https://forums.phpfreaks.com/topic/145269-solved-very-easy-help-getting-current-url/#findComment-762628 Share on other sites More sharing options...
Cory94bailly Posted February 16, 2009 Author Share Posted February 16, 2009 echo $_SERVER[php_SELF] ."?". $_SERVER[QUERY_STRING]; Should give you the entire thing. Ok.. But I don't want http://www.site.com/page.php?action=whatever I wannt page.php?action=whatever... Which will work? $page = basename($_SERVER['PHP_SELF']) ."?". $_SERVER[QUERY_STRING]; //Current page user is on $page = basename($_SERVER['PHP_SELF'] ."?". $_SERVER[QUERY_STRING]); //Current page user is on Link to comment https://forums.phpfreaks.com/topic/145269-solved-very-easy-help-getting-current-url/#findComment-763242 Share on other sites More sharing options...
trq Posted February 16, 2009 Share Posted February 16, 2009 Why don't you try them and see? Link to comment https://forums.phpfreaks.com/topic/145269-solved-very-easy-help-getting-current-url/#findComment-763264 Share on other sites More sharing options...
printf Posted February 16, 2009 Share Posted February 16, 2009 I would do it like this... because (PHP_SELF) can be externally modified whereas script_name and path_translated are system / environment defined variables (INTERNALLY DEFINED BY THE SERVER) <?php $_SERVER = array_merge ( $_SERVER, $_ENV ); $url = $_SERVER['HTTPS'] != 'off' ? 'https://' : 'http://'; $url .= $_SERVER['SERVER_NAME']; if ( ! empty ( $_SERVER['SCRIPT_NAME'] ) ) { $url .= $_SERVER['SCRIPT_NAME']; } else { $url .= str_replace ( str_replace ( '\\', '/', $_SERVER['DOCUMENT_ROOT'] ), '', $_SERVER['PATH_TRANSLATED'] ); } if ( ! empty ( $_SERVER['QUERY_STRING'] ) ) { $url .= '?' . $_SERVER['QUERY_STRING']; } echo $url; ?> Link to comment https://forums.phpfreaks.com/topic/145269-solved-very-easy-help-getting-current-url/#findComment-763272 Share on other sites More sharing options...
Cory94bailly Posted February 16, 2009 Author Share Posted February 16, 2009 I would do it like this... because (PHP_SELF) can be externally modified whereas script_name and path_translated are system / environment defined variables (INTERNALLY DEFINED BY THE SERVER) <?php $_SERVER = array_merge ( $_SERVER, $_ENV ); $url = $_SERVER['HTTPS'] != 'off' ? 'https://' : 'http://'; $url .= $_SERVER['SERVER_NAME']; if ( ! empty ( $_SERVER['SCRIPT_NAME'] ) ) { $url .= $_SERVER['SCRIPT_NAME']; } else { $url .= str_replace ( str_replace ( '\\', '/', $_SERVER['DOCUMENT_ROOT'] ), '', $_SERVER['PATH_TRANSLATED'] ); } if ( ! empty ( $_SERVER['QUERY_STRING'] ) ) { $url .= '?' . $_SERVER['QUERY_STRING']; } echo $url; ?> Wow! Thanks alot.. But I am setting it in my config file so what would I do for the equivalent of this? $page = basename($_SERVER['PHP_SELF']) ."?". $_SERVER[QUERY_STRING]; //Current page user is on (Sorry btw, it's 5:48 AM so I don't even know what's happening..!) Link to comment https://forums.phpfreaks.com/topic/145269-solved-very-easy-help-getting-current-url/#findComment-763277 Share on other sites More sharing options...
trq Posted February 16, 2009 Share Posted February 16, 2009 But I am setting it in my config file so what would I do for the equivalent of this? Just use... $page = basename($_SERVER['PHP_SELF']) . "?" . $_SERVER['QUERY_STRING']; if printf's code isn't working for you. Link to comment https://forums.phpfreaks.com/topic/145269-solved-very-easy-help-getting-current-url/#findComment-763278 Share on other sites More sharing options...
Cory94bailly Posted February 16, 2009 Author Share Posted February 16, 2009 But I am setting it in my config file so what would I do for the equivalent of this? Just use... $page = basename($_SERVER['PHP_SELF']) . "?" . $_SERVER['QUERY_STRING']; if printf's code isn't working for you. That's the thing, I don't know if it works or not.. Also: I would do it like this... because (PHP_SELF) can be externally modified whereas script_name and path_translated are system / environment defined variables (INTERNALLY DEFINED BY THE SERVER) That kinda scares me so his looks a bit more secure if I could use it.. :'( Link to comment https://forums.phpfreaks.com/topic/145269-solved-very-easy-help-getting-current-url/#findComment-763283 Share on other sites More sharing options...
trq Posted February 16, 2009 Share Posted February 16, 2009 That's the thing, I don't know if it works or not.. Then why don't you try it? so his looks a bit more secure if I could use it.. How do you know it looks more secure? You don't know what it does do you? Why don't you try it as well? Link to comment https://forums.phpfreaks.com/topic/145269-solved-very-easy-help-getting-current-url/#findComment-763286 Share on other sites More sharing options...
Cory94bailly Posted February 16, 2009 Author Share Posted February 16, 2009 I used this: $page = basename($_SERVER['PHP_SELF']) . "?" . $_SERVER['QUERY_STRING']; And it seemed to work fine, the only thing is that it puts a ? at the end of index.php even if the user is not on a page with "?" in the url. Is there any way to check if the user is on a page with "?"??? And if they are, add the "?", if not.. Don't add one Link to comment https://forums.phpfreaks.com/topic/145269-solved-very-easy-help-getting-current-url/#findComment-763287 Share on other sites More sharing options...
trq Posted February 16, 2009 Share Posted February 16, 2009 Is there any way to check if the user is on a page with "?" And if they are, add the "?", if not.. Don't add one Yes. A little logical thinking and this is what I came up with. if (count($_SERVER['QUERY_STRING'])) { $page = basename($_SERVER['PHP_SELF']) . "?" . $_SERVER['QUERY_STRING']; } else { $page = basename($_SERVER['PHP_SELF']); } Link to comment https://forums.phpfreaks.com/topic/145269-solved-very-easy-help-getting-current-url/#findComment-763642 Share on other sites More sharing options...
Cory94bailly Posted February 16, 2009 Author Share Posted February 16, 2009 Is there any way to check if the user is on a page with "?" And if they are, add the "?", if not.. Don't add one Yes. A little logical thinking and this is what I came up with. if (count($_SERVER['QUERY_STRING'])) { $page = basename($_SERVER['PHP_SELF']) . "?" . $_SERVER['QUERY_STRING']; } else { $page = basename($_SERVER['PHP_SELF']); } Tried, still has the "?".. Link to comment https://forums.phpfreaks.com/topic/145269-solved-very-easy-help-getting-current-url/#findComment-763660 Share on other sites More sharing options...
trq Posted February 16, 2009 Share Posted February 16, 2009 Sorry, should be.... if (strlen($_SERVER['QUERY_STRING'])) { $page = basename($_SERVER['PHP_SELF']) . "?" . $_SERVER['QUERY_STRING']; } else { $page = basename($_SERVER['PHP_SELF']); } Link to comment https://forums.phpfreaks.com/topic/145269-solved-very-easy-help-getting-current-url/#findComment-763705 Share on other sites More sharing options...
Cory94bailly Posted February 17, 2009 Author Share Posted February 17, 2009 Sorry, should be.... if (strlen($_SERVER['QUERY_STRING'])) { $page = basename($_SERVER['PHP_SELF']) . "?" . $_SERVER['QUERY_STRING']; } else { $page = basename($_SERVER['PHP_SELF']); } Wooh, after 3 days and 2 pages, we got it Thanks.. Solved.. Link to comment https://forums.phpfreaks.com/topic/145269-solved-very-easy-help-getting-current-url/#findComment-764042 Share on other sites More sharing options...
trq Posted February 17, 2009 Share Posted February 17, 2009 Sorry, should be.... if (strlen($_SERVER['QUERY_STRING'])) { $page = basename($_SERVER['PHP_SELF']) . "?" . $_SERVER['QUERY_STRING']; } else { $page = basename($_SERVER['PHP_SELF']); } Wooh, after 3 days and 2 pages, we got it Thanks.. Solved.. Would have been solved alot quicker if you were to actually try things. Link to comment https://forums.phpfreaks.com/topic/145269-solved-very-easy-help-getting-current-url/#findComment-764082 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.