Problem with PHP 5.5 +

Discussion in 'PHP' started by KingCobra, Nov 16, 2011.

  1. #1
    Suppose I have a link: http://www.mysite.com/index.php?id=123

    In PHP 5.3 I can get and store the value of name & id easily like this way:

    $myID = $_GET['id'];  // would return '123'
    $myAge = $_GET['age'];
    Code (markup):
    No error gave me though there is not 'age' value in the url.

    But in PHP 5.5 I can't use my previous code. It displaying error message like this:
    Notice: Undefined index: age in E:\WEB\Server\xampp\htdocs\project\index.php on line 27

    If I use 'isset' like the following way then it display no error message:

    $myID = isset($_GET['id']);  // would return '123'
    $myAge = isset($_GET['age']);
    Code (markup):
    But it store 1 & 0 for $myID & $myAge like true or false.

    So how can I store values getting form url using GET method?
     
    KingCobra, Nov 16, 2011 IP
  2. jestep

    jestep Prominent Member

    Messages:
    3,659
    Likes Received:
    215
    Best Answers:
    19
    Trophy Points:
    330
    #2
    You can cast the value to an int. FALSE would cast as 0, while any other value unless it's been tampered with, would cast the the actual number. This would also prevent a lot of injection attacks.
    $myID = (int)$_GET['id'];

    $myAge = (int)$_GET['age'];
     
    jestep, Nov 16, 2011 IP