marklarah Posted May 27, 2009 Share Posted May 27, 2009 Hi I need to get the the current page and store it as a variable. I realize I could do something like <?php echo basename($_SERVER['SCRIPT_FILENAME']); ?> but if the current page was page.php?a=b or whatever, that would of course only tell me page.php, and I would like to be able to store page.php?a=b. So currently, I am using this <?php $urlarr = explode('/', $_SERVER['REQUEST_URI']); $page = count($urlarr)-1; echo $urlarr[$page]; ?> but it seems terribly inefficient for something that is probably rather simple. How would I do this in a quicker way? Thanks Quote Link to comment Share on other sites More sharing options...
redarrow Posted May 27, 2009 Share Posted May 27, 2009 $_SERVER['QUERY_STRING'] <<< might help? Quote Link to comment Share on other sites More sharing options...
marklarah Posted May 27, 2009 Author Share Posted May 27, 2009 $_SERVER['QUERY_STRING'] <<< might help? hmm okay, but that just gives me a=b Quote Link to comment Share on other sites More sharing options...
redarrow Posted May 27, 2009 Share Posted May 27, 2009 <?php echo $_SERVER['PHP_SELF'].$_SERVER['QUERY_STRING']; ?> Quote Link to comment Share on other sites More sharing options...
marklarah Posted May 27, 2009 Author Share Posted May 27, 2009 <?php echo $_SERVER['PHP_SELF'].$_SERVER['QUERY_STRING']; ?> Thats just doing the same as <?php echo $_SERVER['REQUEST_URI']; ?> ? EDIT: Because my stuff is in a folder, I'm getting /thingy/test.php?a=b or whatever with that :/ Quote Link to comment Share on other sites More sharing options...
redarrow Posted May 27, 2009 Share Posted May 27, 2009 what this do then. EDITED <?php echo substr($_SERVER['REQUEST_URI'],; ?> Quote Link to comment Share on other sites More sharing options...
marklarah Posted May 27, 2009 Author Share Posted May 27, 2009 what this do then. <?php echo substr($_SERVER['REQUEST_URI'],0,; ?> that do /thingy/ Quote Link to comment Share on other sites More sharing options...
redarrow Posted May 27, 2009 Share Posted May 27, 2009 edited <?php echo substr($_SERVER['REQUEST_URI'],; ?> Quote Link to comment Share on other sites More sharing options...
marklarah Posted May 27, 2009 Author Share Posted May 27, 2009 Can't we just find the last "\" in the string in $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"]; and then get the content after that or something? Quote Link to comment Share on other sites More sharing options...
marklarah Posted May 27, 2009 Author Share Posted May 27, 2009 edited <?php echo substr($_SERVER['REQUEST_URI'],; ?> Hmm that actually seems to work thanks very much! someone should probably make a sticky with common FAQs with this in or something anyway thanks man Quote Link to comment Share on other sites More sharing options...
marklarah Posted May 27, 2009 Author Share Posted May 27, 2009 Hang on isn't this 'cheating' as such? We're doing this assuming the folder will always be 7 characters, if there is even a folder. Is there a better way to do it so it will always work? Quote Link to comment Share on other sites More sharing options...
redarrow Posted May 27, 2009 Share Posted May 27, 2009 the reason i never used preg_match because the folder is fixed ok. good luck. Quote Link to comment Share on other sites More sharing options...
redarrow Posted May 27, 2009 Share Posted May 27, 2009 advance way. <?php $path=$_SERVER['REQUEST_URI']; preg_match("/[a-z]{1,100}\.[code=php:0].*/",$path,$matched); print_R( $matched); ?> Quote Link to comment Share on other sites More sharing options...
marklarah Posted May 27, 2009 Author Share Posted May 27, 2009 Well I managed to do it myself nevermind <?php echo substr($_SERVER['REQUEST_URI'],strrpos($_SERVER['REQUEST_URI'], "/")+1); ?> is working for me. Quote Link to comment Share on other sites More sharing options...
redarrow Posted May 27, 2009 Share Posted May 27, 2009 even better way. GAMES ARE OVER BACK TO WORK LOL <?php $path="/redarrow/the_real_way.php?1=yes_you_no"; preg_match("/[a-z]{0,100}\.?[code=php:0].*/",$path,$matched); $result=implode(" ",$matched); echo $result; ?> <?php $path=$_SERVER['PHP_SELF'].$_SERVER['QUERY_STRING']; preg_match("/[a-z]{0,100}\.?[code=php:0].*/",$path,$matched); $result=implode(" ",$matched); echo $result; ?> Quote Link to comment Share on other sites More sharing options...
marklarah Posted May 27, 2009 Author Share Posted May 27, 2009 Exactly why is your way the better way? Quote Link to comment 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.