根据cookie会话创建一个随机的,有序的id?

时间:2012-11-06 21:48:34

标签: php sql cookies registration

我有一个用户注册或注册表单,以允许我的网站的访客成为注册会员。表单由名字,姓氏,电子邮件等组成,操作在提交时发送到process_reg.php。

然后将信息插入到sql表'ptb_registrations'中。但它还没有工作,因为我试图从cookie会话中随机生成一个新的用户ID。我不确定我做的是不对的。人们可以在我错的地方纠正我,或指出我正确的方向。感谢。

这是我的process_reg.php脚本:

<?php
require_once("session.php"); 
require_once("functions.php");
require('_config/connection.php');
?>
<?php 

session_start();

include '_config/connection.php'; 


function get_user_id() {
    global $connection;
    global $email;
    $query = "SELECT *
                FROM ptb_registrations
                WHERE email = \"$email\"
                ";
        $user_id_set = mysql_query($query, $connection);
        confirm_query($user_id_set);
        return $user_id_set;
        }
?>
<?php
$user_id_set = get_user_id();
while ($user_id = mysql_fetch_array($user_id_set)) {
    $cookie1 = "{$user_id["id"]}";
    setcookie("ptb_registrations", $cookie1, time()+3600);  /* expire in 1 hour */

}
?>
<?

$first_name = $_POST['first_name'];
$last_name = $_POST['last_name'];
$email = $_POST['email'];
$display_name = $_POST['display_name'];
$date_of_birth = $_POST['date_of_birth'];
$contact_number = $_POST['contact_number'];
$station = $_POST['station'];
$hobbies = $_POST['hobbies'];
$gender = $_POST['gender'];
$password = $_POST['password'];








$result = mysql_query("SELECT first_name FROM ptb_registrations WHERE id=".$cookie1['user_id']."");
$result = mysql_query("SELECT last_name FROM ptb_registrations WHERE id=".$cookie1['user_id']."");
$result = mysql_query("SELECT display_name FROM ptb_registrations WHERE id=".$cookie1['user_id']."");
$result = mysql_query("SELECT email FROM ptb_registrations WHERE id=".$cookie1['user_id']."");
$result = mysql_query("SELECT contact_number FROM ptb_registrations WHERE id=".$cookie1['user_id']."");
$result = mysql_query("SELECT date_of_birth FROM ptb_registrations WHERE id=".$cookie1['user_id']."");
$result = mysql_query("SELECT hobbies FROM ptb_registrations WHERE id=".$cookie1['user_id']."");
$result = mysql_query("SELECT station FROM ptb_registrations WHERE id=".$cookie1['user_id']."");
$result = mysql_query("SELECT gender FROM ptb_registrations WHERE id=".$cookie1['user_id']."");
$result = mysql_query("SELECT password FROM ptb_registrations WHERE id=".$cookie1['user_id']."");






if(!$result) 
{ 
echo "The username you entered does not exist"; 
} 
else 
if($email!= mysql_result($result, 0)) 
{ 
echo ""; 


    $sql=mysql_query("INSERT ptb_registrations SET first_name='$first_name' WHERE id=".$cookie1['user_id'].""); 
    $sql=mysql_query("INSERT ptb_registrations SET last_name='$last_name' WHERE id=".$cookie1['user_id'].""); 
    $sql=mysql_query("INSERT ptb_registrations SET display_name='$display_name' WHERE id=".$cookie1['user_id'].""); 
    $sql=mysql_query("INSERT ptb_registrations SET email='$email' WHERE id=".$cookie1['user_id'].""); 
    $sql=mysql_query("INSERT ptb_registrations SET contact_number='$contact_number' WHERE id=".$cookie1['user_id'].""); 
    $sql=mysql_query("INSERT ptb_registrations SET date_of_birth='$date_of_birth' WHERE id=".$cookie1['user_id'].""); 
    $sql=mysql_query("INSERT ptb_registrations SET hobbies='$hobbies' WHERE id=".$cookie1['user_id'].""); 
    $sql=mysql_query("INSERT ptb_registrations SET station='$station' WHERE id=".$cookie1['user_id'].""); 
    $sql=mysql_query("INSERT ptb_registrations SET gender='$gender' WHERE id=".$cookie1['user_id'].""); 
    $sql=mysql_query("INSERT ptb_registrations SET password='$password' WHERE id=".$cookie1['user_id'].""); 


}
    if($sql) 
    { 
    echo "<div class=\"infobox-profile\"> <strong>Thank You</strong> - We have received your registration details.</div>"; 
    }
else
{ 
echo "<div class=\"infobox-profile\"> <strong>Oooops!</strong> - Something went wrong. <a href=\"../edit_contact_number.php\">Click here</a> to try again.</div>"; 
}  
?>
<? ob_flush(); ?>

<style>
.infobox-profile {
    background-color: #e1e0f7;
    font-family: Arial, Helvetica, sans-serif;
    font-size: 14px;
    color: #181469;
    padding-top: 20px;
    padding-right: 10px;
    padding-bottom: 20px;
    padding-left: 70px;
    margin-bottom: 20px;
    background-image: url(../img/icons/info-icon.png);
    background-repeat: no-repeat;
    background-position: 15px center;
    border-top-width: 1px;
    border-top-style: solid;
    border-top-color: #8f8fdb;
    border-right-width: 1px;
    border-bottom-width: 1px;
    border-left-width: 1px;
    border-right-style: solid;
    border-bottom-style: solid;
    border-left-style: solid;
    border-right-color: #8f8fdb;
    border-bottom-color: #8f8fdb;
    border-left-color: #8f8fdb;
    border-radius: 4px;
    width: 385px;
    margin-left:8px;
}
</style>

1 个答案:

答案 0 :(得分:1)

随机生成的数字并不总是完全随机的,一段时间后,您将始终再次获得相同的数字。这是因为这些数字是由PSEUDO随机数生成器生成的。因为您使用mysql数据库,所以您的问题的最佳解决方案是使用自动增量属性。每次将新用户添加到数据库时,此字段将增加一个。

以下是一个例子:

CREATE TABLE animals (
     id MEDIUMINT NOT NULL AUTO_INCREMENT,
     name CHAR(30) NOT NULL,
     PRIMARY KEY (id)
) ENGINE=MyISAM;