检测WordPress中的会话/ cookie变量,以防止访问文档


Detect session/cookie variable in wordpress to prevent access to documents

嘿伙计们,我已经了解了下面的代码,但我正在尝试通过检测会话变量或cookie或其他东西在我的wordpress博客上创建一个"安全"的AJAX搜索表单

<?php
@session_start();
If (!array_key_exists(‘authed’, $_SESSION))
{
     include ‘not_authed.inc’;
     exit();
}
// go about your business.
?>

我正在尝试将其添加到此:

<?php
function checkValues($value)
{
     // Use this function on all those values where you want to check for both sql injection and cross site scripting
     //Trim the value
     $value = trim($value);
    // Stripslashes
    if (get_magic_quotes_gpc()) {
        $value = stripslashes($value);
    }
     // Convert all &lt;, &gt; etc. to normal html and then strip these
     $value = strtr($value,array_flip(get_html_translation_table(HTML_ENTITIES)));
     // Strip HTML Tags
     $value = strip_tags($value);
    // Quote the value
    $value = mysql_real_escape_string($value);
    return $value;
}   
mysql_connect ("mysql.*****.com", "****","$*****")  or die (mysql_error());
mysql_select_db ("***********");
$term = checkValues($_REQUEST['val']);
$term = mysql_real_escape_string($term);
$sql = mysql_query("select * FROM patient_db WHERE id_number = '$term'");

if($row = mysql_fetch_array($sql)) {
    echo "<img src='"******'" class='leftfloat' border=0>";
    echo '<p>';
    echo '<br /> ID Number: '   .$row['id_number'];
    echo '<br /> Name: '        .$row['Name'];
    echo '<br /> Exp. Date: '   .$row['exp_date'];
    echo '<br /> DOB: '         .$row['dob'];
    echo '</p>';
    //echo "<a href='******' title='Printer Friendly Version' alt='Printer Friendly Version'><img src='"*****'" class='rightfloat' border=0 height=33 width=33></a>";
} else {
    echo "<img src='"*****'" height=50 width=50 class='leftfloat' border=0>";
    print "<h1>USER ID <br/>NOT FOUND</h1><br />";
    print "<strong>OOPS!! THIS COULD BE AN ERROR</strong><br />";
    print "<br />";
    print "<div>*****</div>";
}
?>
您将

遇到的问题是AJAX请求是一个单独的会话/cookie,因为它是一个完全不同的进程,与浏览器无关。

那么,您如何对某人进行身份验证呢?某种令牌。因此,您将创建一个哈希,该哈希需要存储在用户的数据库中,可以在登录等时重新生成。然后,您将使用此令牌来验证该用户并允许 AJAX 提交正常工作。

希望这能为你滚动。因此,在您的 AJAX 推送脚本中,您只需将一个变量附加到名为 token 的 GET 或 POST 数据中,然后在接收 PHP 脚本上检查它。还有其他方法可以做到这一点,这只是我知道的一种:)