Welcome to HBH! If you have tried to register and didn't get a verification email, please using the following link to resend the verification email.

A clean function to help stop XSS. - PHP Code Bank


A clean function to help stop XSS.
A clean function to help stop XSS.
                <?php
	
/* Made by TheMasterSinner */
	
	// helps stop stuff like XSS
	function clean($input)
	{
		if (is_array($input))
		{
			foreach ($input as $key => $val) {
				$output[$key] = clean($val);
				// $output[$key] = $this->clean($val);
			}
		}
		else
		{
			$output = (string) $input;
			
			// if magic quotes is on then use strip slashes
			if (get_magic_quotes_gpc()) {
				$output = stripslashes($output);
			}
			
			// $output = strip_tags($output);
			$output = htmlentities($output, ENT_QUOTES, 'UTF-8');
		}
		
		// return the clean text
		return $output;
	}
	
?>
            
Comments
ghost's avatar
ghost 15 years ago

Very simple and clean. Nice recursion, too, on the "if is_array, foreach, clean(value)"… Checking for magic quotes is a must on any host, and it's a simple addition, so good call there, too. Well done!