Forum Moderators: coopster

Message Too Old, No Replies

Waaah! I got stuck in big bunch of if's

preg_replace, if's, where to place...

         

John_Keates

4:28 pm on Jul 4, 2005 (gmt 0)

10+ Year Member



Here's my code:

if(isset($_GET['p'])){
$id1 = $_GET['p'];

$pattern = '/[^a-zA-z_-]/';
$id = preg_replace($pattern, "", $id1);

$id = $id1;
//mysql get that page if it exists code...
include("mysqli_connect.inc.php");
$psql = "SELECT * FROM page_id WHERE id = " . $id . "";
$result = $mysqli->query($psql);
$page = $filesdir. "home.php";
if($data = $result->fetch_array(MYSQLI_ASSOC)){

$id = $data['id'];
$file = $data['file'];
if(file_exists($filesdir . $file)){
$page = $filesdir . $file . "";

}else{
$page = $filesdir. "home.php";
};
}else{
$page = $filesdir. "home.php";
};
}else{
$page = $filesdir. "home.php";
};

this big if should set $page to the page specifyed with $_GET['p']
as a number. so?p=10 is okay but?p=10a not. The preg_replace should filter everything that isn't a number out and pass the result to $id. then the script should try to get the page that is linked with the id and be included in the page via $page. If the number is not found in the database the defult page: home.php will be used. Or if there is another error the home.php should be used too.

Short: use a number to get the page, on error display home.php

I can't seem to get the preg_replace working...

John

dreamcatcher

6:17 pm on Jul 4, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



PHP already has been in functions to check if a variable is numeric. Try the is_numeric() [uk2.php.net] function, that might help.

Also, try the character class :digit:

if (eregi("^[[:digit:]]+$", $data))
{
//number
}
else
{
//not number
}

dc

jatar_k

6:25 pm on Jul 4, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



you can also use the ctype functions [php.net]

mcibor

8:24 pm on Jul 4, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



To get id I use variable type conversion - I extort integer

$id = (int)$_GET["id"];

this gives you the id you want or 0.
765 => 765
765kljh => 765
7jhg65jhg => 7
kjhg0983 => 0
0009 => 9

Hope this helps. (I even don't check if it exists, because
"" => 0 )

Best regards
Michal Cibor

John_Keates

12:58 pm on Jul 5, 2005 (gmt 0)

10+ Year Member



You all are too good :P
Always answering my n00b questions, many thanks.

John