help me to solve this problem

Discussion in 'PHP' started by Mr.only, Feb 12, 2011.

  1. #1
    i have a script Make pages like this

    http://mysite.com/page.php?id=45
    and in the db just 50 pages
    and the problem is : if i called the page like
    http://mysite.com/page.php?id=654654646

    it's open with any data ..

    so please help me to make reduction

    i need redirect the pages that i don't create in my db to home page
    and redirect this page "http://mysite.com/page.php" to home page too..
     
    Mr.only, Feb 12, 2011 IP
  2. MyVodaFone

    MyVodaFone Well-Known Member

    Messages:
    1,048
    Likes Received:
    42
    Best Answers:
    10
    Trophy Points:
    195
    #2
    You need to put some kind of a check to see if the number (id) exists, so show us your current sql statement or to give you an idea here's some code:

    
    
    $id = $_GET['id'];
    
    $check = mysql_query("SELECT * FROM table WHERE id='$id';") or die(mysql_error());
    if (mysql_num_rows($check) == 0) { // not found
        header("Location: http://mysite.com/page.php"); // go here
    } else {
     // current code here, which loads a page
    }
    
    
    PHP:
     
    MyVodaFone, Feb 12, 2011 IP
  3. edison8301

    edison8301 Peon

    Messages:
    32
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Yes, i do agree, just check for the id first, when it satisfy none zero results then you can continue to next process. Have a try
     
    edison8301, Feb 13, 2011 IP
  4. tvoodoo

    tvoodoo Active Member

    Messages:
    239
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    53
    #4
    Try something like this :
    
    if(isset($_GET['id']) && strlen($_GET['id']) > 0)
    {
    $result = mysql_query("SELECT * FROM pages WHERE id = '".filter_var($_GET['id'],FILTER_SANITIZE_NUMBER_INT)."'");
    if(mysql_num_rows($result) > 0)
    {
    //page exists
    } else {
    header("Location:http://mysite.com/page.php");
    }
    }
    
    PHP:
     
    tvoodoo, Feb 18, 2011 IP