如何合法使用他的个人资料?
问题描述:
我有$ _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时,是否有另一种方式来检查用户注册?谢谢。
答
试试这种方法。
<?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");
}
?>
在此代码中,如果$ _GET ['id']'没有名为'id'的会话变量,将不会被检查。这是你的意图吗? – halfer 2012-03-15 20:01:33
为什么你检查会话,但然后使用$ _GET? – 2012-03-15 20:01:59
顺便说一下,这里有一个很大的安全漏洞 - SQL注入。代之以:'$ url_auth =(int)$ _GET ['id']'。 – halfer 2012-03-15 20:02:32