如何检测用户是否接受了条款?

时间:2014-03-24 00:01:08

标签: php mysql sql

基本上我正在编写一个PHP和MYSQL脚本,用于检查用户是否已接受条款和条件。在数据库中,已注册的每个当前用户都设置为“未接受”。当他们登录到他们被定向到的第一页时,应该有一个scirpt,检测tos表中users列的状态是否设置为“已接受”或“未接受” ”。如果它被接受,他们可以继续,如果不是,他们将被迫转到页面并接受它们,然后才能继续使用我的网站的其余部分。这是到目前为止的代码,但它似乎没有工作。任何建议都有帮助。

<?php

$username=$_SESSION['username'];
$connect = mysql_connect('**', '**', '**', '**');
if (!$connect) 
{
    die('Could not connect: ' . mysql_error());
}
if (!mysql_select_db('**')) 
{
    die('Could not select database: ' . mysql_error());
}
$toschecker = mysql_query("SELECT `tos` FROM `users` WHERE `username` = '$username'");
if (!$toschecker) 
{
    die('Could not query:' . mysql_error());
}
mysql_close($connect);
$unaccepted='unaccepted';

if ($toschecker === $unaccepted)
{
    header('Location: accepttos.php');
}    
?>

由于某些原因,这并没有将他们引导到accepttos.php页面。提前谢谢。

3 个答案:

答案 0 :(得分:1)

将MySQL更改为MySQLi。解释在评论中。

<?php

$username = $_SESSION['username'];
$connect = mysqli_connect('Host', 'Username', 'Password', 'Database');
if (!$connect) 
{
    die('Could not connect: ' . mysql_error());
}

$toschecker = mysqli_query($connect,"SELECT `tos` FROM `users` WHERE `username` = '$username'"); /* SELECT TOS COLUMN */

while ($row = mysqli_fetch_array($toschecker)) 
{
    $tos = $row['tos']; /* STORE TO A VARIABLE THE FETCHED TOS */
}

$unaccepted = 'unaccepted';

if ($tos == $unaccepted) /* COMPARE THE TOS VARIABLE IF UNACCEPTED */
{
    header('Location: accepttos.php');
}

else {
    header('Location: acceptedTOS.php'); /*  IF TOS IS ACCEPTED. CHANGE THE LOCATION */
}

mysqli_close($connect);

?>

答案 1 :(得分:0)

$toschecker是一种资源,请在mysql_query行下尝试以下内容,查看您的查询结果,您可以使用该结果重定向accepttos.php等。

if( $row = mysql_fetch_assoc($toschecker)) {
    var_export($row['tos']);
}

答案 2 :(得分:0)

您需要访问数据库字段中的信息。这是一个例子。

$toschecker = mysql_query($sql);

$row = mysql_fetch_array($toschecker);

$tos = $row['tos'];

if($tos == 'accepted'){
     // go to regular page
}else {
     // go to accept terms page
}