PHP setcookie正在添加百分号

时间:2011-06-30 06:13:25

标签: php mysql sql select

所以,我有一个简单的脚本,允许您在任何“团队名称”之间进行选择。当你选择和总结时,它应该用一个选择的值做一个php setcookie,这是团队名称的哈希版本。

以下是相关代码:

<?php
include 'include/db.php';

    if(isset($_POST['submitteam'])) {
        $team_hash = $_POST['teams'];
        setcookie('ver_aet', $team_hash, time()+2592000);
        header('Location: index.php');
    }

    $email = $_COOKIE['ver_ame'];


        //Find the User Id from the Email Hash
        $sql_finduid = "SELECT * FROM users_sensitive WHERE email_hash = '$email'";
        $sql_finduid_result = mysql_query($sql_finduid);
        while ($row = mysql_fetch_array($sql_finduid_result)) {
            $user_id = $row['user_id']; 
        } //End Find User Id
        /*
        $sql_finduid = mysql_query("SELECT user_id FROM users WHERE email = '$email'");
        $user_id = mysql_result($sql_finduid) or die(mysql_error());
        */

        //Find the Team Id from the User Id above
        $sql_findteams = "SELECT * FROM team_members WHERE user_id = '$user_id'";
        $sql_findteams_result = mysql_query($sql_findteams);
        if(mysql_num_rows($sql_findteams_result) < 1){
                header('Location: registerteam.php?ver_ame=' . $email);
            } else {

        while ($row = mysql_fetch_array($sql_findteams_result)) {
            $team_id = $row['team_id'];
            /*
            $sql_finduid = mysql_query("SELECT user_id FROM users WHERE email = '$email'");
            $user_id = mysql_result($sql_finduid) or die(mysql_error());
            */



            if((mysql_num_rows($sql_findteams_result)) <= 1) {

                $sql_findteamname = "SELECT * FROM teams WHERE team_id = '$team_id'";
                $sql_findteamname_result = mysql_query($sql_findteamname);
                while ($row = mysql_fetch_array($sql_findteamname_result)) {
                    $team_name = $row['team_name'];
                    $team_hash = $row['team_name_hash'];
                }

                setcookie('ver_aet', $team_hash, time()+2592000);
                header('Location: index.php');

            } else {

    //setcookie('ver_ame', $teamname_hash, time()+2592000);
    //setcookie('ver_aet', $email, time()+2592000);
    //header('Location: index.php'); ?>

和HTML

Select the team you would like to view: <br />
            <form method="post" action="<?PHP echo  $_SERVER['PHP_SELF']; ?>">
                <select name="teams">
                    <?PHP           
                    $sql_findteams = "SELECT * FROM team_members WHERE user_id = '$user_id'";
                    $sql_findteams_result = mysql_query($sql_findteams);
                    while ($row = mysql_fetch_array($sql_findteams_result)) {
                        $team_id = $row['team_id'];
                        /*
                        $sql_finduid = mysql_query("SELECT user_id FROM users WHERE email = '$email'");
                        $user_id = mysql_result($sql_finduid) or die(mysql_error());
                        */

                        $sql_findteamname = "SELECT * FROM teams WHERE team_id = '$team_id'";
                        $sql_findteamname_result = mysql_query($sql_findteamname);
                        while ($row = mysql_fetch_array($sql_findteamname_result)) {
                            $team_name = $row['team_name'] . "&nbsp;";
                            $team_hash = $row['team_name_hash'] . "<br />"; 
                        ?>
                        <option value="<?= $team_hash; ?>"><?= $team_name . $team_hash; ?></option>
                <?PHP
                        } 
                    } 
                ?>
                </select>
                <input type="submit" name="submitteam" value="Submit" />
            </form>
        </div>
    </div>
</div>

基本上,“如果点击了提交按钮,请为团队名称设置cookie。如果没有点击,请继续。找到您的电子邮件的cookie,找出您所属的团队数量,如果有一个团队,使你的团队成为cookie并继续,如果没有,则显示所有可用的团队并允许用户选择一个。循环“

我目前有$ team_hash回显只是为了表明它正在拉动正确的哈希值(并且它是)。当我点击提交时,它会循环到页面顶部并执行setcookie语句。它设置了一个cookie但是cookie在设置之后似乎最终会在其中随机出现百分号。

应该设置什么:d2fea5c982b6cb3f5bffc4998d96cbe5 实际设置:d2fea5c982b6cb3f5bffc4998d96cbe5%3Cbr +%2F%3E

这些额外的东西来自哪里?

2 个答案:

答案 0 :(得分:2)

问题是,当您执行<br />时,您在哈希末尾添加$team_hash = $row['team_name_hash'] . "<br />";,并且当您设置选项的值时,您正在使用{{ 1}}包含$team_hash。当您执行<br />时,set cookie会对网址进行编码,从而将其归结为Cookie的结尾。

简单地将行更改为:

<br />

答案 1 :(得分:0)

你有&lt; br /&gt;在某种程度上,PHP是url编码它。

就在这里

$team_hash = $row['team_name_hash'] . "<br />";