在使用数据库登录时重定向到用户特定的URL,并随着用户的进展更新此URL


Redirect to user specific URL upon login using database and update this as user progresses

我正在创建一个网站,用户可以完成一些特别优惠以获得各种奖励,但我正在努力如何存储用户的进度。

我设想如果用户完成提供1和2,当他们下次登录时,他们将提供3。你可以把每个报价想象成梯子上的一个梯级,并且必须连续竞争。

为了让事情更清楚;目前,当用户登录时,他们被重定向到提供1的页面,这是伟大的。但是让我们假设他们在完成几个报价后离开网站,然后再回到网站,他们将再次提供1,这不是那么好。我试着记录他们的进度。

(我认为一个潜在的解决方案)来解决这个问题,我已经在我的数据库中创建了一个列称为"重定向",默认情况下,这将是example.com/offer1。我需要从数据库中召回,并在用户登录时重定向到此URL。并且,每次报价完成后,此URL将根据需要更新,以便保存下一次登录进度。

因此,当用户第一次登录时,他们将被发送到第一个报价(example.com/offer1)然后,在完成第一个报价后,他们将按"继续下一个报价",然后重定向到报价2,并将触发一个脚本将该用户的重定向URL更新为example.com/offer2 -这样,如果他们离开网站并再次返回,他们将在报价2。在报价2完成后,他们按"继续下一个报价",一个类似的脚本将触发更新URL到example.com/offer3 -通过这种方式,用户的进度将被存储。

简而言之,我在问(1)如何在登录到重定向列中的URL时重定向到用户,(2)如何在按下按钮时更新该URL。或者是否有更有效的方法来实现这一点(也许使用会话)。

下面是我当前的登录脚本,它工作得很好(base.php有部分连接到数据库),目前它在登录时指向example.com/offer1,或者如果一个会话已经存在,用户点击登录框。我如何将其更改为从数据库的重定向列查找用户URL呢?然后我怎么能编码一个脚本来更新这个URL在数据库中的每个报价完成后?

    <?php include "base.php"; ?>
<!DOCTYPE html PUBLIC "
<html xmlns="http://www.w3.org/1999/xhtml">  
<head>  
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />  
<title>User Management System </title>
<link rel="stylesheet" href="style.css" type="text/css" />
</head>  
<body>  
<div id="main">
<?php
if(!empty($_SESSION['LoggedIn']) && !empty($_SESSION['Username']))
{
     ?>
    "<meta http-equiv="refresh" content="0;URL='http://example.com/offer1'" />"

    <ul>
        <li><a href="logout.php">Logout.</a></li>
    </ul>
    <?php
}
elseif(!empty($_POST['username']) && !empty($_POST['password']))
{
     $username = mysql_real_escape_string($_POST['username']);
    $password = md5(mysql_real_escape_string($_POST['password']));
     $checklogin = mysql_query("SELECT * FROM users WHERE Username = '".$username."' AND Password = '".$password."'");
    if(mysql_num_rows($checklogin) == 1)
    {
         $row = mysql_fetch_array($checklogin);
        $email = $row['EmailAddress'];
        $_SESSION['Username'] = $username;
        $_SESSION['EmailAddress'] = $email;
        $_SESSION['LoggedIn'] = 1;
         echo "<h1>Success</h1>";
        echo "<p>We are now redirecting you to the member area.</p>";
        ?>
    "<meta http-equiv="refresh" content="0;URL='http://example.com/offer1'" />"
<?php       
    }
    else
    {
         echo "<h1>Error</h1>";
        echo "<p>Sorry, your account could not be found. Please <a href='"index.php'">click here to try again</a>.</p>";
    }
}
else
{
    ?>
   <h1>Member Login</h1>
   <p>Thanks for visiting! Please either login below, or <a href="register.php">click here to register</a>.</p>
    <form method="post" action="index.php" name="loginform" id="loginform">
    <fieldset>
        <label for="username">Username:</label><input type="text" name="username" id="username" /><br />
        <label for="password">Password:</label><input type="password" name="password" id="password" /><br />
        <input type="submit" name="login" id="login" value="Login" />
    </fieldset>
    </form>
   <?php
}
?>
</div>
</body>
</html>

注:我假设存储报价编号的列名为"offer",它位于名为"users"的表中。

<标题> 警告
  • 你正在使用mysql扩展为你的登录代码,这是不赞成的。使用mysqli代替。
  • 要散列用户的密码,您使用md5,它可以被暴力破解攻击。使用crypt()代替。

您可以使用此登录代码解决这些问题(显然,在您使用crypt()对所有密码进行散列之后-要做到这一点,请参阅我的回答的最后一点)

    <?php include "base.php"; ?>
<!DOCTYPE html PUBLIC "
<html xmlns="http://www.w3.org/1999/xhtml">  
<head>  
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />  
<title>User Management System </title>
<link rel="stylesheet" href="style.css" type="text/css" />
</head>  
<body>  
<div id="main">
<?php
$con = mysqli_connect("localhost","root","","your_db");
if(!empty($_SESSION['LoggedIn']) && !empty($_SESSION['Username']))
{
     ?>
    "<meta http-equiv="refresh" content="0;URL='http://example.com/offer1'" />"

    <ul>
        <li><a href="logout.php">Logout.</a></li>
    </ul>
    <?php
}
elseif(!empty($_POST['username']) && !empty($_POST['password']))
{
     $username = mysqli_real_escape_string($_POST['username']);
    $password = md5(mysqli_real_escape_string($_POST['password']));
     $checklogin = mysqli_query($con,"SELECT * FROM users WHERE Username = '".$username."'");
    $row = mysqli_fetch_array($checklogin);
    if(mysqli_num_rows($checklogin) == 1 AND crypt($password, $row['Password']) == $row['Password'])
    {
        $email = $row['EmailAddress'];
        $_SESSION['Username'] = $username;
        $_SESSION['EmailAddress'] = $email;
        $_SESSION['LoggedIn'] = 1;
         echo "<h1>Success</h1>";
        echo "<p>We are now redirecting you to the member area.</p>";
        ?>
    "<meta http-equiv="refresh" content="0;URL='http://example.com/offer1'" />"
<?php       
    }
    else
    {
         echo "<h1>Error</h1>";
        echo "<p>Sorry, your account could not be found. Please <a href='"index.php'">click here to try again</a>.</p>";
    }
}
else
{
    ?>
   <h1>Member Login</h1>
   <p>Thanks for visiting! Please either login below, or <a href="register.php">click here to register</a>.</p>
    <form method="post" action="index.php" name="loginform" id="loginform">
    <fieldset>
        <label for="username">Username:</label><input type="text" name="username" id="username" /><br />
        <label for="password">Password:</label><input type="password" name="password" id="password" /><br />
        <input type="submit" name="login" id="login" value="Login" />
    </fieldset>
    </form>
   <?php
}
?>
</div>
</body>
</html>

登录时重定向到正确的报价

你需要编辑这个:

?>
"<meta http-equiv="refresh" content="0;URL='http://example.com/offer1'" />"
<?php

echo '<meta http-equiv="refresh" content="0;URL=''http://example.com/offer'.$row['offer'].'''" />';

更新按钮点击的值

1)创建一个php文件,并放入以下代码:
<?php
session_start();
$con = mysqli_connect("localhost","root","","your_db");
$select = mysqli_fetch_assoc(mysqli_query($con,"SELECT offer FROM users      WHERE Username = '".$_SESSION['username']."'"));
$plus = $select['offer']++;
mysqli_query($con,"UPDATE users SET offer=".$plus);
header("location: http://example.com/offer".$plus);
?>

2)将按钮指向刚刚创建的文件

用crypt()对先前存储的所有密码进行散列

启动此代码一次 (强烈建议在启动前备份数据库)

<?php
$con = mysqli_connect("localhost","root","","your_db");
function Casual($length=22){
    $characters ="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890";
    $code = "";
    for($i = 0; $i<$length; $i++){
        $code = $code.substr($characters,rand(0,strlen($characters)-1),1);
    }
    return $code;
}
$query = mysqli_query($con,"SELECT Password,Username FROM users");
while($assoc = mysqli_fetch_assoc($query)){
    $newcode= '$2a$07$'.Casual(22).'$';
$hashed_password = crypt($assoc['Password'],$newcode);
mysqli_query($con,"UPDATE users SET Password='".$hashed_password."' WHERE Username='".$assoc['Username']."'");
}
?>