dtwzwmv87399 2014-03-24 00:01
浏览 7
已采纳

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

Basically I am writing a PHP and MYSQL script that will check whether a user has accepted the terms and conditions or not. In the databse every current user that has signed up is set to "unaccepted". When they log in the first page that they are directed to should have a scirpt on it that detects whether or not the status of the tos column in the users table is set to "accepted" or "unaccepted". If it is accepted they can continue, and if it is not they they will be forced to go to a page and accept them before they can continue to use the rest of my site. This is the code so far but it doesn't seem to be working. Any suggestions help.

<?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');
}    
?>

For some reason this isn't directing them to the accepttos.php page. Thanks in advance.

  • 写回答

3条回答 默认 最新

  • dongzan9069 2014-03-24 00:11
    关注

    Change MySQL to MySQLi. Explanations are in the comments.

    <?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);
    
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?