2012-03-15 43 views
0

我有$ _GET来验证他的网站所有者的问题。如何合法使用他的个人资料?

<?php 
session_start(); 

include('scripts/db_connect.php'); 

    if(isset($_SESSION['id'])){ 
     $url_auth = $_GET['id']; 

    }else{ 
     echo "no user found"; 
    exit(); 
} 

$sql = "SELECT * FROM table WHERE id='".$url_auth."'"; 
$query = $db->query($sql); 
if($query->num_rows !=1){ 
    header("Location: index.php"); 
    exit(); 

我有问题,读出的是$ _GET ID:

IM使用此代码来检查,如果用户ID的注册资格与否。似乎有什么问题,我不知道为什么。当有人在浏览器中调用任何ID时,是否有另一种方式来检查用户注册?谢谢。

+0

在此代码中,如果$ _GET ['id']'没有名为'id'的会话变量,将不会被检查。这是你的意图吗? – halfer 2012-03-15 20:01:33

+0

为什么你检查会话,但然后使用$ _GET? – 2012-03-15 20:01:59

+0

顺便说一下,这里有一个很大的安全漏洞 - SQL注入。代之以:'$ url_auth =(int)$ _GET ['id']'。 – halfer 2012-03-15 20:02:32

回答

1

试试这种方法。

<?php 
session_start(); 

include('scripts/db_connect.php'); 

$id=mysql_escape_string($_GET['id']); //Sanitized the variable to avoid SQL Injection attacks 

$sql = "SELECT * FROM table WHERE id='".$id."'"; 
$query = $db->query($sql); 
if($query->num_rows !=1){ 
    header("Location: index.php"); 
    exit(); 
} 
else 
{ 
$_SESSION["loggedIn"]="Success"; 
header("authenticationSuccess.php"); 
} 
?> 

现在在您的所有其他页面上检查此$_SESSION["loggedIn"]="Success"以检查用户是否为真品。

变化的评论:

如果你真的认为$_GET的问题,试试这个。

<?php 
    session_start(); 
    @extract($_GET); 
    include('scripts/db_connect.php'); 

    $sql = "SELECT * FROM table WHERE id='".mysql_escape_string($id)."'"; 
    $query = $db->query($sql); 
    if($query->num_rows !=1){ 
     header("Location: index.php"); 
     exit(); 
    } 
    else 
    { 
    $_SESSION["loggedIn"]="Success"; 
    header("authenticationSuccess.php"); 
    } 
    ?> 
+0

你好,谢谢你的回答。我试图使用它。但我得到转发到index.php。似乎我有一些困难,从ID读出$ _GET。 – John 2012-03-15 20:16:49

+0

只是为了检查..你为什么不尝试硬编码的价值,看看你的查询是否正确执行? $ sql = SELECT * FROM table WHERE id = whateveryouridis; – 2012-03-15 20:24:20

+0

查询正在工作。获得正确的ID。这将以正确的方式显示在网址中。 – John 2012-03-15 20:41:55

相关问题