注册表格并为其设置cookie [英] Registration form and set cookies for it

查看:80
本文介绍了注册表格并为其设置cookie的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

要求:



创建一个PHP应用程序,允许用户注册网站并输入要存储在cookie中的个人资料信息。

第一部分:注册页面

创建一个名为register.php的页面,该页面显示一个用户注册表单,其中包含来自customer表的以下字段:

电子邮件地址 - 必填,有效的电子邮件,maxlength 60

名字 - 必填,maxlength 40

姓氏 - 必填,maxlength 20

密码 - 必填,8-20字符

确认密码 - 必填,8-20个字符,必须匹配密码

从类型表中读取记录,并使用名称作为标签将每个记录显示为复选框和genreId作为值。指导用户最多选择3种喜欢的流派。用户必须至少选择1种类型且不超过3种。

添加一个值为Register的提交按钮。

提交表单时,验证信息并显示错误消息如果有任何错误。

第二部分:创建cookie

如果表格中的数据全部有效,则按如下方式创建cookie:

名称:表格中的名字和姓氏,中间有空格,到期日:从当前日期起30天

genre1:用户选择的第一个类型,到期:从当前日期起1年

genre2:用户选择的第二个类型,到期:从当前日期起1年。如果没有做出选择,请不要创建cookie。

genre3:用户选择的第三个类型,到期:从当前日期起1年。如果没有做出选择,请不要创建cookie。

优惠券:value = .25,临时cookie。

转移到index.html页面(参见下一步)

第三部分:索引页面

创建一个名为index.php的页面

为html添加页眉和页脚函数

在标题之后,检查名称cookie是否存在。如果是,请显示消息:欢迎,姓名!其中name被替换为名称cookie的值。

检查优惠券cookie是否存在。如果是这样,请显示消息:'今天订购以获得整个订单的25%折扣!'。

检查genre1 cookie是否存在。如果是,请获取值并检查genre2和genre3 cookies并获取这些值(如果存在)。创建一个查询,从轨道表中选择最多20个轨道,其类型与用户输入的类型之一相匹配。(即选择*来自其中genreId = 1的轨道或者genreId = 2或genreId = 3限制20)。

如果不存在genre1 cookie,只需创建一个简单的查询来选择前20个曲目(从曲目限制20中选择*)

执行sql查询。

在页面上,显示标题:你可能喜欢的曲目

列出名称,genreId,作曲家和单价您的数据集中有20个跟踪。如果货币不是美元,请乘以数组中的转换因子并以首选货币显示。

包含指向register.php页面的链接。



我尝试过:



Requirements:

Create a PHP application to allow a user to register for the website and enter profile information to be stored in cookies.
Part I: Registration page
Create a page called register.php that displays a user registration form with the following fields from the customer table:
Email address - required, valid email, maxlength 60
First Name - required, maxlength 40
Last Name - required, maxlength 20
Password - required, 8-20 characters
Confirm Password - required, 8-20 characters, must match Password
Read the records from the genre table and display each one as a checkbox using the name as the label and the genreId as the value. Instruct the user to select up to 3 favorite genres. User must select at least 1 genre and no more than 3.
Add a submit button with a value of Register.
When the form is submitted, validate the information and display error messages if there are any errors.
Part II: Create cookies
If the data from the form is all valid, then create cookies as follows:
name: first and last names from the form with a space in between, expires: 30 days from current date
genre1: the first genre selected by the user, expires: 1 year from current date
genre2: the second genre selected by the user, expires: 1 year from current date. If no selection was made, do not create cookie.
genre3: the third genre selected by the user, expires: 1 year from current date. If no selection was made, do not create cookie.
coupon: value = .25, temporary cookie.
Transfer to the index.html page (see next step)
Part III: Index page
Create a page called index.php
Add your header and footer functions for the html
After the header, check to see if the name cookie exists. If so, display the message: Welcome, name! where name is replaced with the value of the name cookie.
Check to see if the coupon cookie exists. If so, display the message: 'Order today to receive 25% off your entire order!".
Check to see if the genre1 cookie exists. If so, get the value and check for the genre2 and genre3 cookies and get those values also if they exist. Create a query to select a maximum of 20 tracks from the tracks table that have a genre that matches one of the genre's entered by the user. (i.e. Select * from tracks where genreId = 1 or genreId = 2 or genreId = 3 limit 20).
If no genre1 cookie exists, just create a simple query to select the first 20 tracks (select * from tracks limit 20)
Execute the sql query.
On the page, display a heading: "Tracks you may like"
List the Name, genreId, composer and unit price for the 20 tracks in your dataset. If the currency is anything other than US Dollars, multiply by the conversion factor from the array and display in the preferred currency.
Include a link to the register.php page.

What I have tried:

<?php
$conn = mysqli_connect("localhost", "root", "","chinook");
        if (isset($_POST['submit'])) {
        $valid = true;
if (isset($_POST['submit'])) {
$valid = true;

		$email = htmlspecialchars($_POST['email']);
        if (empty($email)) {
            echo "<p class="error">Please enter your email address</p>";
            $valid = false;
}}
		setcookie('email',$email,time()+60*60*7);
	
		if (!preg_match('/[-\w.]+@([A-z0-9][-A-z0-9]+\.)+[A-z]{2,4}/',$email)) {
            echo "<p class="error">Invalid email address</p>";
        }
        $firstname = htmlspecialchars(trim($_POST['firstname']));
        if (empty($firstname)) {
            echo "<p class="error">Please enter your first name</p>";
            $valid = false;
        }
        $lastname = htmlspecialchars(trim($_POST['lastname']));
        if (empty($lastname)) {
            echo "<p class="error">Please enter your last name</p>";
            $valid = false;
        }
       
        $username = htmlspecialchars(trim($_POST['username']));
        if (empty($username)) {
            echo "<p class="error">Please enter a username</p>";
            $valid = false;
        }
	
        $password = htmlspecialchars(trim($_POST['password']));
        if (empty($password)) {
            echo "<p class="error">Please enter a password</p>";
            $valid = false;
        }
		  if (!preg_match('/^(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?!.*\s).{8,15}$/',$password)) {
            echo "<p class="error">Password must have 1 uppercase letter, 1 lowercase letter, 1 number and be 8-15 characters in length</p>";
        }
		if (strlen($password)<8 or strlen($password)>20) {
            echo "<p class="error">password must be between 6 and 12 characters in length</p>";
            $valid = false;
        }
		$confirmpassword = htmlspecialchars(trim($_POST['confirmpassword']));
        if (empty($confirmpassword)) {
            echo "<p class="error">Please enter a confirm password</p>";
            $valid = false;
        }
       
        if (strcmp($password,$confirmpassword)!=0) {
            echo "<p class="error">Passwords do not match</p>";
            $valid = false;
        }
		if (isset($_POST['Genre'])) {
        $playlists = $_POST['Genre'];
        } 
		else {
            echo "<p class="error">Please choose Genre.</p>";
            $valid = false;
            $Genre[0]="";
        }
		if (isset($_POST['register'])) {
            setcookie('firstname', $_POST['firstname'], time()+ (60 * 60 * 24 * 30), "/");
            setcookie('lastname',($_POST['lastname']), time()+ (60 * 60 * 24 * 30), "/");
        
			setcookie('Genre1', md5($_POST['Genre1']), time()+(60*60*24*365),"/");
			setcookie('Genre2', md5($_POST['Genre2']), time()+(60*60*24*365),"/");
			setcookie('Genre3', md5($_POST['Genre3']), time()+(60*60*24*365),"/");
			
			//setcookie('coupon', .25($_POST['coupon']), time()+ "/");
        } else {
            setcookie('firstname', $_POST['firstname'], false, '/');
            setcookie('lastname', md5($_POST['lastname']), false, '/');
        }
        if ($valid) {
            header("Location: index1.php?firstname=$firstname&lastname=$lastname");
            exit();
        }
		
    } else {
        $firstname="";
        $lastname="";
        $email="";
        $username="";
        $password="";
        $confirmpassword="";
		$Genre="";
    }
?>

 <p>
    Email address:
    
</p>
<p>
    First name:
    
</p>
<p>
    Last name:
    
</p>

<p>
    Username:
    
</p>
<p>
    Password:
    
</p>
<p>
	Confirm Password:
    
</p>
<p>
	Genre:
<?php
$conn = mysqli_connect("localhost", "root", "",'chinook');
$query = "Select name from Genre";
$result = mysqli_query($conn,$query);
if (!$result) {
die(mysqli_error($conn)); }
while ($row = mysqli_fetch_assoc($result)){
echo "".$row['name'];
}
?>

</p><p>
    
</p>

推荐答案

conn = mysqli_connect( localhost,< span class =code-string> root chinook);
if (isset(
conn = mysqli_connect("localhost", "root", "","chinook"); if (isset(


_POST [' submit'])){
_POST['submit'])) {


有效 =真;
if (isset(


这篇关于注册表格并为其设置cookie的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆