当前位置:首页 > PHP教程 > php应用 > 列表

PHP实现简单注册登录系统

发布:smiling 来源: PHP粉丝网  添加日期:2022-04-07 07:55:44 浏览: 评论:0 

本文实例为大家分享了PHP实现简单注册登录系统的具体代码,供大家参考,具体内容如下。

目录结构如下,其中function文件夹下包含两个函数文件,uploads文件夹用于存放上传的文件。

PHP实现简单注册登录系统

注:博主使用的是php5,使用php7的小伙伴运行报错的话有一部分原因是新的语法造成的,修改成新语法就可以了

html页面

登录页面

index.html

  1. <form action="login_process.php" method="POST"
  2.  登录 
  3.  <input type="text" name="userName" size="20" maxlength="15" placeholder="请填写用户名及域名"
  4.  <br> 
  5.  
  6.  注册 
  7.  <input type="password" name="password" size="20" maxlength="15"
  8.  <br> 
  9.  <input type="submit" value="登录"
  10.  <input type="button" onclick="window.location.href='register.html'" value="注册"
  11. </form> 

注册页面

register.html

  1. <h2>用户注册登录系统</h2> 
  2. <hr> 
  3. <form action="register.php" method="POST" enctype="multipart/form-data"> 
  4.  用户名: 
  5.  <input type="text" name="userName" size="20" maxlength="15" placeholder="必须填写用户名"> 
  6.  @ 
  7.  <select name="domain" id=""> 
  8.  <option value="@163.com" selected>163.com</option> 
  9.  <option value="@126.com">126.com</option> 
  10.  </select> 
  11.  <br> 
  12.  
  13.  登录密码: 
  14.  <input type="password" name="password" size="20" maxlength="15"> 
  15.  <br> 
  16.    
  17.  确认密码: 
  18.  <input type="password" name="confirmPassword" size="20" maxlength="15"> 
  19.  <br> 
  20.  
  21.  选择性别: 
  22.  <input type="radio" name="sex" value="male" checked>男 
  23.  <input type="radio" name="sex" value="female">女 
  24.  <br> 
  25.  
  26.  个人爱好: 
  27.  <input name="interests[]" type="checkbox" value="music">音乐 
  28.  <input name="interests[]" type="checkbox" value="game">游戏 
  29.  <input name="interests[]" type="checkbox" value="film">电影 
  30.  <br> 
  31.  
  32.  个人相片 
  33.  <input type="hidden" name="MAX_FILE_SIZE" value="1024"> 
  34.  <input type="file" name="myPicture" size="25" maxlength="100"> 
  35.  <br> 
  36.  
  37.  备注信息: 
  38.  <textarea name="remark" cols="30" rows="4" placeholder="请填写备注信息"></textarea> 
  39.  <br> 
  40.  
  41.  <input type="submit" name="submit" value="注册"> 
  42.  <input type="reset" name="cancel" value="重填"> 
  43. </form> 

功能实现文件

实现登录功能

login_process.php

  1. <?php 
  2.  include_once("function/database.php"); 
  3.  // $userName = $_POST['userName']; 
  4.  // $password = $_POST['password']; 
  5.  $userName = addslashes($_POST['userName']); 
  6.  $password = addslashes($_POST['password']); 
  7.  getConnect(); 
  8.  $loginSQL = "select * from users where userName='$userName' and password='$password'"
  9.  echo $loginSQL
  10.  $resultLogin = mysql_query($loginSQL); 
  11.  if (mysql_num_rows($resultLogin) > 0) { 
  12.  echo "登录成功"
  13.  } else { 
  14.  echo "登录失败"
  15.  } 
  16.  closeConnect(); 
  17. ?> 

实现注册功能

register.php

  1. <?php 
  2.  include_once("function/fileSystem.php"); 
  3.  include_once("function/database.php"); 
  4.  
  5.  if (emptyempty($_POST)) { 
  6.  exit("您提交的表单数据超过post_max_size! <br>"); 
  7.  } 
  8.  
  9.  // 判断输入密码与确认密码是否相同 
  10.  $password = $_POST['password']; 
  11.  $confirmPassword = $_POST['confirmPassword']; 
  12.  if ($password != $confirmPassword) { 
  13.  exit("输入的密码与确认密码不相等!"); 
  14.  } 
  15.  
  16.  $userName = $_POST['userName']; 
  17.  $domain = $_POST['domain']; 
  18.  $userName = $userName . $domain
  19.  
  20.  // 判断用户名是否重复 
  21.  $userNameSQL = "select * from users where userName = '$userName'"
  22.  getConnect(); 
  23.  $resultSet = mysql_query($userNameSQL); 
  24.  if (mysql_num_rows($resultSet) > 0) { 
  25.  exit("用户名已被占用,请更换其他用户名"); 
  26.  } 
  27.  
  28.  $sex = $_POST['sex']; 
  29.  if (emptyempty($_POST['interests'])) { 
  30.  $interests = ""
  31.  } else { 
  32.  $interests = implode(";"$_POST['interests']); 
  33.  } 
  34.  
  35.  $remark = $_POST['remark']; 
  36.  $myPictureName = $_FILES['myPicture']['name']; 
  37.  
  38.  $registerSQL = "insert into users values(null, '$userName', '$password', '$sex', '$interests', '$myPictureName', '$remark')"
  39.  $message = upload($_FILES['myPicture'], "uploads"); 
  40.  
  41.  if ($message == "上传成功" || $message == "没有上传") { 
  42.  mysql_query($registerSQL); 
  43.  $userID = mysql_insert_id(); 
  44.  echo "注册成功<br>"
  45.  } else { 
  46.  exit($message); 
  47.  } 
  48.  
  49.  $userSQL = "select * from users where user_id = '$userID'"
  50.  $userResult = mysql_query($userSQL); 
  51.  if ($user = mysql_fetch_array($userResult)) { 
  52.  echo "您的注册用户名为:" . $user['userName']; 
  53.  } else { 
  54.  exit("用户注册失败!"); 
  55.  } 
  56.  closeConnect(); 

函数文件(function文件夹)

实现数据库连接与关闭的函数

database.php

  1. <?php 
  2.  $databaseConnection = null; 
  3.  function getConnect() { 
  4.  $hosthome = "localhost"
  5.  $database = "register"
  6.  $userName = "root"
  7.  $password = "123456"
  8.  global $databaseConnection
  9.  $databaseConnection = @mysql_connect($hosthome$userName$passwordor die (mysql_error()); 
  10.  mysql_query("set names gbk"); 
  11.  @mysql_select_db($database$databaseConnectionor die (mysql_error()); 
  12.  } 
  13.    
  14.  function closeConnect() { 
  15.  global $databaseConnection
  16.  if ($databaseConnection) { 
  17.  @mysql_close($databaseConnectionor die (mysql_error()); 
  18.  } 
  19.  } 
  20. ?> 

实现文件上传的函数

fileSystem.php

  1. <?php 
  2.  function upload($file$filePath) { 
  3.  $error = $file['error']; 
  4.  switch ($error) { 
  5.  case 0: 
  6.  $fileName = $file['name']; 
  7.  $fileTemp = $file['tmp_name']; 
  8.  $destination = $filePath . "/" . $fileName
  9.  move_uploaded_file($fileTemp$destination); 
  10.  return "上传成功"
  11.  case 1: 
  12.  return "上传超过upload_max_filesize"
  13.  case 2: 
  14.  return "上传文件超过form的MAX_FILE_SIZE"
  15.  case 3: 
  16.  return "附件部分上传"
  17.  case 4: 
  18.  return "没有上传"
  19.  } 
  20.  } 
  21. ?>

Tags: PHP注册登录

分享到: