FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


What can I replace the fuction eregi with? (php)





Possum
The Function eregi is now Deprecated.

I use it to validate input fields on a form. What can I replace it with..

How would you check input fields to make sure the inputed data is OK and SAFE.

Code:
<?
session_start();
function checkOK($field)
{
if (eregi("\r",$field) || eregi("\n",$field)){
die("Invalid Input!");
}
}
$title=$_POST['title'];
checkOK($title);



Thank you..
Nemesis234
depends what you mean by "safe and ok"

what html is allowed? numbers, letters, special chars etc..?

does it get submitted to sql db?

does it have a set max length?
Possum
Nemesis234 wrote:
depends what you mean by "safe and ok"

what html is allowed? numbers, letters, special chars etc..?

does it get submitted to sql db?

does it have a set max length?


All very good good questions.

As I am learning php and HTML forms in general. I am interested in the answers to all the points you mention..

Thank you..
Nemesis234
well, to clean text of html and prevent sql injection i use


Code:
<?
session_start();
function checktext($field)
   {
      $field = strip_tags($field);
        $field = mysql_real_escape_string($field);
      return $field;
   }
$title=$_POST['title'];

$title = checkOK($title);

this wont through an error if they have submitted bad input, it will simply remove all the "bad" bits from thier input

if i wanted to throw an error i would use this, much more simple
Code:
function checktext($field)
   {
         if (preg_match('/[^0-9A-Z a-z]/', $field)){
            die("Invalid Input!");
         }
   }

basically if they put in anything other then 0-9 a-z or a space, through an error

or to check numbers, i use
Code:
function checknumber($field)
   {
         if (preg_match('/[^0-9]/', $field)){
            die("Invalid Input!");
         }
   }

allows all numbers, but no negative sign
Fire Boar
eregi should be replaced with preg_match or strpos. Often strpos is enough. In your example,

Code:
<?php
session_start();
function checkOK($field)
{
  if (strpos($field, "\n") !== false || strpos($field, "\r") !== false)
  {
    die("Invalid Input!");
  }
}
$title = $_POST['title'];
checkOK($title);
Related topics
Formulieren
Can I refresh a page using php?
How to make Forum a homepage?
WAMP Help :( - UNRESOVED!
JavaScript in PHP
[Resolved]How to use php to replace a javascript variable?
php need help:how to replace these strings ?
PHP: Directory listing code
simple php problem, please help...
PHP Toturail
PHP IP Ban
PHP Validation Class
PHP Editing Question
php script in php nuke - need help!
Reply to topic    Frihost Forum Index -> Scripting -> Php and MySQL

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.