如何在PHP中将所有页面连接到我的数据库

时间:2010-10-28 08:31:51

标签: php

我创建了4个文件

  1. signup.php
  2. signup_ac.php
  3. confirmation.php
  4. config.php
  5. 另外,我创建了2个数据库:

    1. temp_members_db
    2. registered_members
    3. 我想这样做:

      1. 用户注册时。随机一组确认码。

      2. 将其信息和确认代码保存在表“temp_members_db”中。这是临时表,我们必须在验证电子邮件地址后将此信息移至表“registered_members”。

      3. 将数据成功插入表“temp_membes_db”后,向用户注册的电子邮件发送确认链接,如果电子邮件无效,他们将不会收到我们的电子邮件。

      4. 他们必须点击确认链接才能激活他们的帐户。 (将数据从表“temp_member_db”移动到表“registered_members”并在此步骤中从表“temp_members_db”中删除数据)

      5. 注册表格:

        <table width="350" border="0" align="center" cellpadding="0" cellspacing="0">
        <tr>
        <td><form name="form1" method="post" action="signup_ac.php">
        <table width="100%" border="0" cellspacing="4" cellpadding="0">
        <tr>
        <td colspan="3"><strong>Sign up</strong></td>
        </tr>
        <tr>
        <td width="76">Name</td>
        <td width="3">:</td>
        <td width="305"><input name="name" type="text" id="name" size="30"></td>
        </tr>
        <tr>
        <td>E-mail</td>
        <td>:</td>
        <td><input name="email" type="text" id="email" size="30"></td>
        </tr>
        <tr>
        <td>password</td>
        <td>:</td>
        <td><input name="password" type="password" id="password" size="30"></td>
        </tr>
        <tr>
        <td>Country</td>
        <td>:</td>
        <td><input name="country" type="text" id="country" size="30"></td>
        </tr>
        <tr>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
        <td><input type="submit" name="Submit" value="Submit"> &nbsp;
        <input type="reset" name="Reset" value="Reset"></td>
        </tr>
        </table>
        </form></td>
        </tr>
        </table>
        

        SIGNUP_AC表格:

        <?
        include('config.php');
        
        // table name 
        $tbl_name=temp_members_db;
        
        // Random confirmation code 
        $confirm_code=md5(uniqid(rand())); 
        
        // values sent from form 
        $name=$_POST['name'];
        $email=$_POST['email'];
        $country=$_POST['country'];
        
        // Insert data into database 
        $sql="INSERT INTO $tbl_name(confirm_code, name, email, password, country)VALUES('$confirm_code', '$name', '$email', '$password', '$country')";
        $result=mysql_query($sql);
        
        // if suceesfully inserted data into database, send confirmation link to email 
        if($result){
        
        // ---------------- SEND MAIL FORM ----------------
        
        // send e-mail to ...
        $to=$email;
        
        // Your subject
        $subject="Your confirmation link here";
        
        // From
        $header="from: your name <your email>";
        
        // Your message
        $message="Your Comfirmation link \r\n";
        $message.="Click on this link to activate your account \r\n";
        $message.="http://www.yourweb.com/confirmation.php?passkey=$confirm_code";
        
        // send email
        $sentmail = mail($to,$subject,$message,$header);
        
        }
        
        // if not found 
        else {
        echo "Not found your email in our database";
        }
        
        // if your email succesfully sent
        if($sentmail){
        echo "Your Confirmation link Has Been Sent To Your Email Address.";
        }
        else {
        echo "Cannot send Confirmation link to your e-mail address";
        }
        
        ?>
        
        **CONFIRMATION FORM**
        
        <?
        include('config.php');
        
        // Passkey that got from link 
        $passkey=$_GET['passkey'];
        
        $tbl_name1="temp_members_db";
        
        // Retrieve data from table where row that match this passkey 
        $sql1="SELECT * FROM $tbl_name1 WHERE confirm_code ='$passkey'";
        $result1=mysql_query($sql1);
        
        // If successfully queried 
        if($result1){
        
        // Count how many row has this passkey
        $count=mysql_num_rows($result1);
        
        // if found this passkey in our database, retrieve data from table "temp_members_db"
        if($count==1){
        
        $rows=mysql_fetch_array($result1);
        $name=$rows['name'];
        $email=$rows['email'];
        $password=$rows['password']; 
        $country=$rows['country']; 
        
        $tbl_name2="registered_members";
        
        // Insert data that retrieves from "temp_members_db" into table "registered_members" 
        $sql2="INSERT INTO $tbl_name2(name, email, password, country)VALUES('$name', '$email', '$password', '$country')";
        $result2=mysql_query($sql2);
        }
        
        // if not found passkey, display message "Wrong Confirmation code" 
        else {
        echo "Wrong Confirmation code";
        }
        
        // if successfully moved data from table"temp_members_db" to table "registered_members" displays message "Your account has been activated" and don't forget to delete confirmation code from table "temp_members_db"
        if($result2){
        
        echo "Your account has been activated";
        
        // Delete information of this user from table "temp_members_db" that has this passkey 
        $sql3="DELETE FROM $tbl_name1 WHERE confirm_code = '$passkey'";
        $result3=mysql_query($sql3);
        
        }
        
        }
        ?>
        
        **CONFIG**
        
        <?
        
        $host="localhost"; // Host name 
        $username="root"; // Mysql username 
        $password="abhijits"; // Mysql password 
        $db_name="temp_members_db"; // Database name 
        
        
        //Connect to server and select database.
        mysql_connect("$host", "$username", "$password")or die("cannot connect to server"); 
        mysql_select_db("$db_name")or die("cannot select DB");
        
        ?>
        

        表“temp_members_db”

        CREATE TABLE `temp_members_db` (
        `confirm_code` varchar(65) NOT NULL default '',
        `name` varchar(65) NOT NULL default '',
        `email` varchar(65) NOT NULL default '',
        `password` varchar(15) NOT NULL default '',
        `country` varchar(65) NOT NULL default ''
        ) ENGINE=MyISAM DEFAULT CHARSET=latin1;
        

        表“registered_members”

        CREATE TABLE `registered_members` (
        `id` int(4) NOT NULL auto_increment,
        `name` varchar(65) NOT NULL default '',
        `email` varchar(65) NOT NULL default '',
        `password` varchar(65) NOT NULL default '',
        `country` varchar(65) NOT NULL default '',
        PRIMARY KEY (`id`)
        ) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
        

2 个答案:

答案 0 :(得分:3)

是的,不要那样做。在成员表中添加另一个字段,指示其地址是否已经过验证。

答案 1 :(得分:1)

您是否考虑过采取不同的方法?您可以拥有一个“用户”表,其中一个字段指示它们是否已“授权”。当用户注册时,他们的信息会添加到“用户”表中,“授权”字段设置为“否”或留空。同时,获取用户输入的数据的子集,例如他们的电子邮件地址和他们在“用户”表中的ID,并将其添加到数组中,即:

$user[email]=user@domain.com;
$user[id]=1;

然后加密 - 你可以简单地序列化然后base64编码结果。

base64_encode(serialize($user));

这将为您提供“授权码”。将用户(您可以使用PHPMailer)的电子邮件发送到激活页面,并将此加密链接分配给名为“授权”的变量。当用户单击链接并转到授权页面时,使用GET获取此变量,base 64 decode然后反序列化以获取底层数组。

unserialize(base64_decode($user));

然后获取用户[id]变量并更新“用户”表中的“授权”字段。

它确实是一种不同的方法,但它可能有助于减少一些后端基础设施。