登录  /  注册

如何用PHP实现简单注册登录系统(附源码)

慕斯
发布: 2021-05-25 15:19:25
转载
9247人浏览过

 上篇文章给大家介绍了《如何用php解决高并发问题?(附源码)》,本文继续给大家介绍如何用php实现简单注册登录系统 ,一起研究吧!!!      

如何用PHP实现简单注册登录系统(附源码)

目录结构如下,其中function文件夹下包含两个函数文件,uploads文件夹用于存放上传的文件。
文档结构
注:博主使用的是php5,使用php7的小伙伴运行报错的话有一部分原因是新的语法造成的,修改成新语法就可以了


html页面

登录页面

index.html

登录后复制
    登录          
    注册          
         

注册页面

register.html

<h2>用户注册登录系统</h2>
<hr>
登录后复制
    用户名:          @          
    登录密码:          
         确认密码:          
    选择性别:     男     女     
    个人爱好:     音乐     游戏     电影     
    个人相片               
    备注信息:          
         

功能实现文件

实现登录功能

login_process.php

<?php     include_once("function/database.php");
    // $userName = $_POST[&#39;userName&#39;];
    // $password = $_POST[&#39;password&#39;];
    $userName = addslashes($_POST[&#39;userName&#39;]);
    $password = addslashes($_POST[&#39;password&#39;]);
    getConnect();
    $loginSQL = "select * from users where userName=&#39;$userName&#39; and password=&#39;$password&#39;";
    echo $loginSQL;
    $resultLogin = mysql_query($loginSQL);
    if (mysql_num_rows($resultLogin) > 0) {
        echo "登录成功";
    } else {
        echo "登录失败";
    }
    closeConnect();
?&gt;
登录后复制

实现注册功能

register.php

<?php     include_once("function/fileSystem.php");
    include_once("function/database.php");

    if (empty($_POST)) {
        exit("您提交的表单数据超过post_max_size! <br>");
    }

    // 判断输入密码与确认密码是否相同
    $password = $_POST['password'];
    $confirmPassword = $_POST['confirmPassword'];
    if ($password != $confirmPassword) {
        exit("输入的密码与确认密码不相等!");
    }

    $userName = $_POST['userName'];
    $domain = $_POST['domain'];
    $userName = $userName . $domain;

    // 判断用户名是否重复
    $userNameSQL = "select * from users where userName = '$userName'";
    getConnect();
    $resultSet = mysql_query($userNameSQL);
    if (mysql_num_rows($resultSet) &gt; 0) {
        exit("用户名已被占用,请更换其他用户名");
    }

    $sex = $_POST['sex'];
    if (empty($_POST['interests'])) {
        $interests = "";
    } else {
        $interests = implode(";", $_POST['interests']);
    }

    $remark = $_POST['remark'];
    $myPictureName = $_FILES['myPicture']['name'];

    $registerSQL = "insert into users values(null, '$userName', '$password', '$sex', '$interests', '$myPictureName', '$remark')";
    $message = upload($_FILES['myPicture'], "uploads");

    if ($message == "上传成功" || $message == "没有上传") {
        mysql_query($registerSQL);
        $userID = mysql_insert_id();
        echo "注册成功<br>";
    } else {
        exit($message);
    }

    $userSQL = "select * from users where user_id = '$userID'";
    $userResult = mysql_query($userSQL);
    if ($user = mysql_fetch_array($userResult)) {
        echo "您的注册用户名为:" . $user['userName'];
    } else {
        exit("用户注册失败!");
    }
    closeConnect();
登录后复制

函数文件(function文件夹)

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

database.php

<?php     $databaseConnection = null;
    function getConnect() {
        $hosthome = "localhost";
        $database = "register";
        $userName = "root";
        $password = "123456";
        global $databaseConnection;
        $databaseConnection = @mysql_connect($hosthome, $userName, $password) or die (mysql_error());
        mysql_query("set names gbk");
        @mysql_select_db($database, $databaseConnection) or die (mysql_error());
    }
    
    function closeConnect() {
        global $databaseConnection;
        if ($databaseConnection) {
            @mysql_close($databaseConnection) or die (mysql_error());
        }
    }
?>
登录后复制

实现文件上传的函数

fileSystem.php

<?php     function upload($file, $filePath) {
        $error = $file[&#39;error&#39;];
        switch ($error) {
            case 0:
                $fileName = $file[&#39;name&#39;];
                $fileTemp = $file[&#39;tmp_name&#39;];
                $destination = $filePath . "/" . $fileName;
                move_uploaded_file($fileTemp, $destination);
                return "上传成功";
            case 1:
                return "上传超过upload_max_filesize";
            case 2:
                return "上传文件超过form的MAX_FILE_SIZE";
            case 3:
                return "附件部分上传";
            case 4:
                return "没有上传";
        }
    }
    ?>
登录后复制

推荐学习:《PHP视频教程

以上就是如何用PHP实现简单注册登录系统(附源码)的详细内容,更多请关注php中文网其它相关文章!

智能AI问答
PHP中文网智能助手能迅速回答你的编程问题,提供实时的代码和解决方案,帮助你解决各种难题。不仅如此,它还能提供编程资源和学习指导,帮助你快速提升编程技能。无论你是初学者还是专业人士,AI智能助手都能成为你的可靠助手,助力你在编程领域取得更大的成就。
来源:CSDN网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
关于CSS思维导图的课件在哪? 课件
凡人来自于2024-04-16 10:10:18
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2024 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号