Jump to content

How to restrict a variable to only numeric values?


Recommended Posts

Hello!

 

I recently ran into a problem, and I have no one to ask for help :(

 

I have implemented in one of my pages a free script for pagination, which works as expected if the values it requires are passed as they would normally be.

 

For example, the pagination class requires a variable to be set: ?page= and if its value is numeric then it will display the required page. So this part works.

 

Now, if I request the page like this: http://site.com/index.php?page='1 , notice the ' before the numeric value it will break everything that's beneath the pagination's links, ie: the page footer.

 

I was looking for a way to parse the value set in the page variable and retrieve only the numeric value, if any (if I don't get any value, I'll just display the first page) but I couldn't come up with anything...

 

to get the value from this variable, I have the following script:

<?php
    if (isset($_GET['page']))
    {
        $page =  (int) $_GET['page'];  // it breaks if any other non-numeric values are passed along
    }
?>

 

 

Can someone please help a sista' out?

well if someone is putting something like that in the url then they are probably trying to break it in the first place, and that 1 in '1 would just be some arbitrary value.  Instead of trying to filter out a number in the midst of an obvious attack attempt, simply display a default page number if it's not a positive integer within the page range.

 

 

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.