Fluoresce Posted September 29, 2013 Share Posted September 29, 2013 I have pagination on my website. The URL looks like this: www.example.com/articles/1 The "1" is the page number. At the moment, I have about 15 pages. If someone manually changes the page number to, say, 16, my PHP script throws a 404 "page not found" error, which is what I want: if($pageNumber > $totalPages) { header("HTTP/1.1 404 Not Found"); include('404.php'); die(); } I also want a 404 if someone manually changes the page number to zero. This is quite easy to do: if(($pageNumber > $totalPages) || ($page == 0)) { header("HTTP/1.1 404 Not Found"); include('404.php'); die(); } All is well so far. The problem is that, today, I noticed that if someone changes the page number to 01, they will see Page 1; if they change it to 02, they will see Page 2; and so on. My question therefore is this: How do I throw a 404 if $pageNumber is a 2-or-more-digit number that starts with a zero? Quote Link to comment Share on other sites More sharing options...
requinix Posted September 29, 2013 Share Posted September 29, 2013 Why do you care? It's just a number. Quote Link to comment Share on other sites More sharing options...
Irate Posted September 29, 2013 Share Posted September 29, 2013 You could pass the number to a string function, then check for the first character. Or, better, remove any 0's not preceded by a 0 in a whole. Quote Link to comment Share on other sites More sharing options...
Solution requinix Posted September 29, 2013 Solution Share Posted September 29, 2013 If we're going to just go straight to the answer without caring why this problem apparently exists, After checking that the page number isn't empty $page[0] == "0" Quote Link to comment Share on other sites More sharing options...
Fluoresce Posted September 29, 2013 Author Share Posted September 29, 2013 If we're going to just go straight to the answer without caring why this problem apparently exists, After checking that the page number isn't empty $page[0] == "0" Thanks! Works well. By the way, I just like to be very thorough when it comes to SEO and avoiding duplicate content. I like to block every possible gap. The same content can't appear on more than one page. The way things were, Page 1's contents would appear on pages 01, 001, 0001, 00001, etc. These pages have to give 404 errors for me to relax. Quick newbie question. What's the difference between these? $page[0] == "0" $page[0] == 0 They both seem to work fine. Quote Link to comment Share on other sites More sharing options...
Ch0cu3r Posted September 29, 2013 Share Posted September 29, 2013 (edited) Alternative is regex for detecting infinite leading 0's if(preg_match('~^0+~', $page)) { // do 404 } ^0+ checks if $page starts with a 0 and matches 1 or more times However $page[0] == '0' is just as effective. $page[0] is getting the first character from $page. It'll return true if the first character is 0 What's the difference between these? $page[0] == "0" $page[0] == 0 They both seem to work fine. First one checks if the first character of $page is equal to the string 0 literally (values from urls are treated as strings even if the value is a number). The second one checks to see if $page[0] is false (0 is the same as false). If your want to check for the integer 0 then uses strict comparison $page[0] === 0. This will be false as $page contains a string not a number. Edited September 29, 2013 by Ch0cu3r Quote Link to comment Share on other sites More sharing options...
Fluoresce Posted September 29, 2013 Author Share Posted September 29, 2013 First one checks if the first character of $page is equal to the string 0 literally (values from urls are treated as strings even if the value is a number). The second one checks to see if $page[0] is false (0 is the same as false). If your want to check for the integer 0 then uses strict comparison $page[0] === 0. This will be false as $page contains a string not a number. Thank you for clearing that up! 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.