Jump to content

How to restrict a variable to only numeric values?


julia k

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.

 

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.