用两个参数重写URL

时间:2016-09-26 15:23:20

标签: .htaccess mod-rewrite

我有这个项目结构:

/目录/主/包括

在主文件夹中是index.php。 在include文件夹中是verify.php。

成功插入sql查询后,将发送一封电子邮件。 (的index.php)

在我的index.php中,我使用以下网址发送邮件:

<a href='http://domain.com/dir/main/include/verify.php?user_id=$user_id&tokenCode=$tokenCode'>Click here.</a>

现在,将调用verify.php。我得到'user_id'和'tokenCode':

$user_id   = $_GET['user_id'];
$tokenCode = $_GET['tokenCode'];

嗯,问题是: 首先,我怎么能用.htaccess做到这一点? 其次,.htaccess应该放在哪里?

网址将生成:

require_once __DIR__ . '/../db_config.php';

    // Create connection

    $conn = new mysqli(DB_SERVER, DB_USER, DB_PASSWORD, DB_DATABASE);

    // Check connection

    if ($conn->connect_error)
        {
        die("Connection failed: " . $conn->connect_error);
        }
        // Request type is Register new user
        $username     = $_POST['username'];
        $email    = $_POST['email'];
        $password = $_POST['password'];



    $sql = "SELECT email from user WHERE email = '$email'";
    $result = $conn->query($sql);
    $sql2 = "SELECT username from user WHERE username = '$username'";
    $result2 = $conn->query($sql2);

        if ($result->num_rows > 0) {
            // user existed 
            $response["error"]     = TRUE;
            $response["error_msg"] = "email already existed";
            echo json_encode($response);
        } else if($result2->num_rows > 0){

                // user is already existed - error response
                $response["error"]     = TRUE;
                $response["error_msg"] = "username already existed";
                echo json_encode($response);
        } else {

                $uuser_id               = uniqid('', true);
        $tokenCode          = substr(str_shuffle("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"), 0, 25);
        $salt      = **hided**;
        $salt      = substr($salt, 0, 10);
        $encrypted = **hided**
        $hash      = array(
            "salt" => $salt,
            "encrypted" => $encrypted
        );
        $encrypted_password = $hash["encrypted"]; // encrypted password
        $salt               = $hash["salt"]; // salt

    $sql = "INSERT INTO user(username, email, tokenCode, encrypted_password, salt, created_at) VALUES('$username', '$email', '$tokenCode', '$encrypted_password', '$salt', NOW())";
    $result = $conn->query($sql);
        // check for successful store
        if ($result) {
            $user_id = mysqli_insert_id($conn);

    $sql = "SELECT * FROM user WHERE user_id = $user_id";
    $result = $conn->query($sql);

    while ($row = $result->fetch_assoc())
            {

            // temp user array

                    $response["error"]              = FALSE;
                    $response["user_id"]            = $row["user_id"];
                    $response["user"]["username"]       = $row["username"];
                    $response["user"]["email"]      = $row["email"];
                    $response["user"]["created_at"] = $row["created_at"];
            }
                    echo json_encode($response);

           $message = "                 
                        Hello $username,
                        <br /><br />
                        !<br/>
                        To complete please , just click following link<br/>
                        <br /><br />
                        <a href='http://domain.com/dir/main/include/verify.php?user_id=$user_id&tokenCode=$tokenCode'>Click here.</a>
                        <br /><br />
                        Thanks";



        }

1 个答案:

答案 0 :(得分:1)

RewriteRule

将以下行放在根目录中的.htaccess文件中,

RewriteRule ^verify/id(.*)/token(.*)$ dir/main/include/verify.php?user_id=$1&tokenCode=$2 [NC,L]

更新:更改此部分中的href=""

          $message = "                 
                    Hello $username,
                    <br /><br />
                    !<br/>
                    To complete please , just click following link<br/>
                    <br /><br />
                    <a href='http://domain.com/dir/main/include/verify.php?user_id=$user_id&tokenCode=$tokenCode'>Click here.</a>
                    <br /><br />
                    Thanks";

       $message = "                 
                    Hello $username,
                    <br /><br />
                    !<br/>
                    To complete please , just click following link<br/>
                    <br /><br />
                    <a href='http://domain.com/verify/id$user_id/token$tokenCode'>Click here.</a>
                    <br /><br />
                    Thanks";

summery:表示当用户点击该链接时,该请求将转到http://domain.com/dir/main/include/verify.php,但用户会在浏览器中看到网址栏http://domain.com/verify/id123456/token123456abc

确保将上面的RewriteRule添加到根目录中的.htaccess文件中。