Jump to content

[SOLVED] sanitize string values passed in URLs


robb73

Recommended Posts

I've always used integers when passing values in URLs and then using them with $_GET.

To check the id is OK I've been using:

 

 

                if (isset($_GET['id'])) {

$new_id = (int) $_GET['id'];

        } else {

$new_id = 0;

        }

 

if ($new_id > 0) { // do something

 

                } else {

 

                echo '<p>this page has been accessed in error.</p>';

 

                }

 

However, I'd like to pass strings instead of integers so the URL is a bit more meaningful.

How would I go about checking its OK in the receiving script?

 

 

I'm worried about someone substituting their own value into the url.

 

I usually end up using the value in an SQL query, so I want to make sure its clean before I run the mysqli_query().

 

 

 

Maybe I've got this the wrong way round, should I just use mysqli_real_escape_string on the string value passed to $_GET and then used in a mysqli_query() ?

 

 

Found the answer in a book, I'm just going to test against a regular expression. Posted the solution in case anyone was interested.

 

 

if (eregi('^[[:alpha:]]+$', stripslashes(trim($_GET['string'])))) {

 

    run mysqli_query()

 

} else {

 

    send error

 

}

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.