Jump to content

Recommended Posts

I have a simple script that acquires user input and posts to a MySQL database.  Since the script is offered as a free download, I can't exert any control over the configuration of the target server, nor over which versions of php and MySQL might be installed. register_globals could be on or off; magic(!) quotes could be on or off, version 4 php could be used, etc.

 

Would someone care to suggest a bomb-proof function/snippet for dealing with user-entered single and double quotes and slashes, so that properly 'escaped' data can be passed to the database, regardless of the host server configuration and php/MySQL versions?

Link to comment
https://forums.phpfreaks.com/topic/86780-escaping-input-on-any-server/
Share on other sites

Found this, while trolling ...

 

function clean($value) {
if (get_magic_quotes_gpc()) {
	$value = stripslashes($value);
}
if (!is_numeric($value)) {
	$value = mysql_real_escape_string($value);
}
return $value;
}

array_walk($_POST,'clean');
extract($_POST,EXTR_PREFIX_ALL,'post');

 

Any comments, suggestions, cautions, improvements???

I use

 

<?php

        function clean($data)
        {
            $res = get_magic_quotes_gpc() ? stripslashes($data) : $data;   
            $res = strip_tags($res);
            $res = mysql_real_escape_string($res);
            return $res;
        }

        foreach ($_POST as $k=>$v)
        {
            ${$k} = clean($v);
        }
?>

And I use

<?php
if (get_magic_quotes_gpc()) {
	$_POST = array_map('stripslashesinarray', $_POST);
	$_GET = array_map('stripslashesinarray', $_GET);
	$_COOKIE = array_map('stripslashesinarray', $_COOKIE);
	$_REQUEST = array_map('stripslashesinarray', $_REQUEST);
}

function stripslashesinarray($value) {
	return (is_array($value) ? array_map('stripslashesinarray', $value):stripslashes($value));
}
?>

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.