本篇文章带大家来了解一下怎样使用PHP来实现简单的登录注册的功能,实现这个功能需要用到MySql的相关知识,希望对大家有帮助!
php怎样实现用户的登录注册
最近学习中要使用PHP+MySql实现简单登录注册,看了很多简单案例后发现,并没有人通过PDO实现简单登录注册,要么就是Mysql和MySqli实现,就算实现了意义也不大,js就可以做的事情,没必要放到PHP中去,我却偏偏不相信,保留着一股倔劲,简单的实现了登录注册,下面请看代码,有错误的地方希望大佬们指教!
新建数据库名itcast , 表名user ,执行下面代码
create table user( id int unsigned primary key auto_increment, username varchar(10) not null comment '用户名', password char(20) not null comment '密码', email varchar(40) not null comment '邮箱' )charset=utf8; insert into user (username,password,email) values ('张三','123456','zhangsan@qq.com');
目录结构
首先从注册前台页面register_html.php开始
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>欢迎注册</title> </head> <form action="./register.php" method="post"> <table > <tr><td class="title" colspan="2">欢迎注册新用户</td></tr> <tr><th>用户名:</th><td><input type="text" name="username" /></td></tr> <tr><th>邮箱:</th><td><input type="text" name="email" /></td></tr> <tr><th>密码:</th><td><input type="password" name="password" id="pw1" /></td></tr> <tr><th>确认密码:</th><td><input type="password" id="pw2" /></td></tr> <tr><td colspan="2" class="td-btn"> <input type="submit" value="提交注册" class="button" /> <input type="button" value="返回登录" class="button" onclick="location.href='login.php'" /> </td></tr> </table> </form> </html>
注册后台页面register.php(注册即是新增)
<?php header ('Content-type:text/html;charset=utf-8'); //数据库服务器主机名,端口号,选择的数据库,字符集 $dsn = "mysql:host=localhost;dbname=itcast;charset=utf8"; $user = 'root'; //数据库名 $pwd = 'root'; //数据库密码 try{ $pdo = new PDO($dsn,$user,$pwd); //如果post表单不为空 if(!empty($_POST)){ //声明变量$fields,用来保存字段信息 $fields = array('username','password','email'); //声明$values,用来保存值信息 $values = array(); //遍历$fields,获取输入用户名、密码、邮箱的键和值 foreach($fields as $k=>$v){ $data = isset($_POST[$v]) ? $_POST[$v] : ''; if($data=='') die($v.'字段不能为空!'); //赋值给$fields数组 $fields[$k] = "$v"; //赋值给$values数组 $values[] = "'$data'"; } //将$fields数组以逗号连接,赋值给$fields,组成insert语句中的字段部分 //implode — 将一个一维数组的值转化为字符串 $fields = implode(',', $fields); //将$values数组以逗号连接,赋值给$values,组成insert语句中的值部分 $values = implode(',', $values); //最后把$fields和$values拼接到insert语句中,注意要指定表名 $sql = "insert into user ($fields) values ($values)"; if($res = $pdo->query($sql)){ //注册成功,自动跳转到会员中心 echo '<script>alert("注册成功!");window.location.href="login_html.php";</script>'; }else{ die ('注册失败!'); } } }catch(PDOException $e){ echo $e->getMessage().'<br>'; echo $e->getLine().'<br>'; echo $e->__toString().'<br>'; } define('APP', 'itcast'); require './register_html.php';
登录前台页面login_html.php
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>欢迎登录</title> </head> <body> <form method="post" action="./login.php"> <table> <tr><td>欢迎登录</td></tr> <tr><th>用户名:</th><td><input type="text" name="username" /></td></tr> <tr><th>密码:</th><td><input type="password" name="password" /></td></tr> <tr><td> <input type="submit" value="登录" /> <input type="reset" value="重新填写" /> </td></tr> </table> </form> </body> </html>
登录后台页面login.php
<?php header ('Content-type:text/html;charset=utf-8'); //数据库相关信息 $dsn = "mysql:host=localhost;dbname=itcast;charset=utf8"; $user = 'root'; //数据库名 $pwd = 'root'; //数据库密码,根据自己的密码更改 try{ $pdo = new PDO($dsn,$user,$pwd); //如果表单中不为空 if(!empty($_POST)){ //从表单中获取数据 $username = isset($_POST['username']) ? trim($_POST['username']) : ''; $password = isset($_POST['password']) ? ($_POST['password']) : ''; //执行SQL语句 $sql = "select `id`,`password` from `user` where `username`='$username'"; if($res = $pdo->query($sql)){ //登录成功,自动跳转到会员中心 echo '<script>alert("登录成功");window.location.href="index.php";</script>'; }else{ //否则提示登录失败 die('登录失败!'); } } }catch(PDOException $e){ //这段用于出错的时候,方便告诉我们那里错了 echo $e->getMessage().'<br>'; echo $e->getLine().'<br>'; //显示错误所在多少行 echo $e->__toString().'<br>'; } define('APP', 'itcast'); require './login_html.php'; ?>
最后是我们登录成功的inex.php页面
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Insert title here</title> </head> <body> <h1>登录成功!</h1> </body> </html>
这里做的并不是很美观,大家可以在这个基础上增加好看的样式,增加两次密码是否相同的判断,使用正则实现注册格式的判断等等,这里就不一一讲了,目的主要是能实现简单的登录注册,嘻嘻!
输入注册信息
点击注册
注册成功,自动跳转到登录页面
输入刚刚注册的信息
点击登录
成功跳转到index.php页面
大家如果感兴趣的话,可以点击《PHP视频教程》进行更多关于PHP知识的学习。

php把负数转为正整数的方法:1、使用abs()函数将负数转为正数,使用intval()函数对正数取整,转为正整数,语法“intval(abs($number))”;2、利用“~”位运算符将负数取反加一,语法“~$number + 1”。

实现方法:1、使用“sleep(延迟秒数)”语句,可延迟执行函数若干秒;2、使用“time_nanosleep(延迟秒数,延迟纳秒数)”语句,可延迟执行函数若干秒和纳秒;3、使用“time_sleep_until(time()+7)”语句。

php字符串有下标。在PHP中,下标不仅可以应用于数组和对象,还可应用于字符串,利用字符串的下标和中括号“[]”可以访问指定索引位置的字符,并对该字符进行读写,语法“字符串名[下标值]”;字符串的下标值(索引值)只能是整数类型,起始值为0。

php除以100保留两位小数的方法:1、利用“/”运算符进行除法运算,语法“数值 / 100”;2、使用“number_format(除法结果, 2)”或“sprintf("%.2f",除法结果)”语句进行四舍五入的处理值,并保留两位小数。

判断方法:1、使用“strtotime("年-月-日")”语句将给定的年月日转换为时间戳格式;2、用“date("z",时间戳)+1”语句计算指定时间戳是一年的第几天。date()返回的天数是从0开始计算的,因此真实天数需要在此基础上加1。

在php中,可以使用substr()函数来读取字符串后几个字符,只需要将该函数的第二个参数设置为负值,第三个参数省略即可;语法为“substr(字符串,-n)”,表示读取从字符串结尾处向前数第n个字符开始,直到字符串结尾的全部字符。

方法:1、用“str_replace(" ","其他字符",$str)”语句,可将nbsp符替换为其他字符;2、用“preg_replace("/(\s|\ \;||\xc2\xa0)/","其他字符",$str)”语句。

查找方法:1、用strpos(),语法“strpos("字符串值","查找子串")+1”;2、用stripos(),语法“strpos("字符串值","查找子串")+1”。因为字符串是从0开始计数的,因此两个函数获取的位置需要进行加1处理。


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SublimeText3 English version
Recommended: Win version, supports code prompts!

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

SublimeText3 Mac version
God-level code editing software (SublimeText3)

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.