Jump to content

How do I identify if a number starts with a zero?


Fluoresce
Go to solution Solved by requinix,

Recommended Posts

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?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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 by Ch0cu3r
Link to comment
Share on other sites

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!

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.