HDFilmMaker2112 Posted June 10, 2012 Share Posted June 10, 2012 This is bugging me; it's only a error notice. So when I turn error reporting off it goes away, but I'd like to stop it all together. $url=$_SERVER['REQUEST_URI']; list($junk, $key) = explode("?", $url); The above is giving: Notice: Undefined offset: 1 on Line 5 Line 5 is the list Quote Link to comment Share on other sites More sharing options...
scootstah Posted June 10, 2012 Share Posted June 10, 2012 The error means that there is no numerically-indexed array returned by explode(), which probably means the URL doesn't contain a "?". What exactly are you trying to do? Quote Link to comment Share on other sites More sharing options...
HDFilmMaker2112 Posted June 10, 2012 Author Share Posted June 10, 2012 okay, I'll have to live with it then. It's because I'm using Mod_Rewrite on a few on my URLs. For example domain.com/index.php?signup is accessible as domain.com/signup, but if you type index.php?signup in the address bar it still works. So I'm catching stuff after the "?" as $key and then comparing it to see if $key is equal to the values that I've rewritten with mod_rewrite, and if it is redirecting to the rewritten url. It's included in function.php which I call on some pages that don't have a ? in the URL so it throws a notice on those pages apparently. Quote Link to comment Share on other sites More sharing options...
scootstah Posted June 11, 2012 Share Posted June 11, 2012 The following will give you the query string if one exists: if (array_key_exists('query', $url = parse_url($_SERVER['REQUEST_URI']))) { $key = $url['query']; } Quote Link to comment Share on other sites More sharing options...
smoseley Posted June 11, 2012 Share Posted June 11, 2012 Off topic: Scoots, your signature has a bug: "try" is a reserved word in PHP. Quote Link to comment Share on other sites More sharing options...
scootstah Posted June 11, 2012 Share Posted June 11, 2012 Off topic: Scoots, your signature has a bug: "try" is a reserved word in PHP. Well, we'll just have to pretend it isn't. Quote Link to comment Share on other sites More sharing options...
smoseley Posted June 11, 2012 Share Posted June 11, 2012 You could do something like: if (!$succeed) try { try_again(); } Quote Link to comment Share on other sites More sharing options...
HDFilmMaker2112 Posted June 11, 2012 Author Share Posted June 11, 2012 The following will give you the query string if one exists: if (array_key_exists('query', $url = parse_url($_SERVER['REQUEST_URI']))) { $key = $url['query']; } Works perfect. Quote Link to comment Share on other sites More sharing options...
kicken Posted June 11, 2012 Share Posted June 11, 2012 FYI, The query string (everything after the ?) is available in $_SERVER['QUERY_STRING']. No need to try and parse it out of the request uri. Quote Link to comment Share on other sites More sharing options...
scootstah Posted June 11, 2012 Share Posted June 11, 2012 FYI, The query string (everything after the ?) is available in $_SERVER['QUERY_STRING']. No need to try and parse it out of the request uri. True story. Listen to this man. Quote Link to comment Share on other sites More sharing options...
HDFilmMaker2112 Posted June 11, 2012 Author Share Posted June 11, 2012 FYI, The query string (everything after the ?) is available in $_SERVER['QUERY_STRING']. No need to try and parse it out of the request uri. True story. Listen to this man. I tried using this but it sent the browser into a indefinite loop and eventually ended in a browser alert saying something along the lines of "This page is trying to redirect in a way that will never load.". It would work fine for URLs that have the question mark in them, but I'd end up with the alert on pages that didn't. So index.php?signup would redirect to /signup, but /signup would never actually load because it lacked the question mark. Or atleast that's what it seemed like was going. This is what I was using: if(isset($_SERVER['QUERY_STRING'])){ $key=$_SERVER['QUERY_STRING']; } Using the method you posted I have no issues what so ever. Quote Link to comment Share on other sites More sharing options...
scootstah Posted June 11, 2012 Share Posted June 11, 2012 FYI, The query string (everything after the ?) is available in $_SERVER['QUERY_STRING']. No need to try and parse it out of the request uri. True story. Listen to this man. I tried using this but it sent the browser into a indefinite loop and eventually ended in a browser alert saying something along the lines of "This page is trying to redirect in a way that will never load.". It would work fine for URLs that have the question mark in them, but I'd end up with the alert on pages that didn't. So index.php?signup would redirect to /signup, but /signup would never actually load because it lacked the question mark. Or atleast that's what it seemed like was going. This is what I was using: if(isset($_SERVER['QUERY_STRING'])){ $key=$_SERVER['QUERY_STRING']; } Using the method you posted I have no issues what so ever. $_SERVER['QUERY_STRING'] is always "set", but it will be an empty string if no query string is present. So, you need to check if it is empty and not set. if (!empty($_SERVER['QUERY_STRING'])) { $key = $_SERVER['QUERY_STRING']; } 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.