Home Backend Development PHP Tutorial Cookie control access authorization for beginners learning PHP

Cookie control access authorization for beginners learning PHP

Dec 25, 2017 am 11:54 AM
cookie newbie

In formal projects, there will basically be applications with authorization control, such as RBAC permissions, etc. This article helps novices better understand PHP and use cookies by sharing how to use cookies to control access authorization.

<?php  
    if(isset($_POST[&#39;name&#39;])||isset($_POST[&#39;pass&#39;])){  
        //如果有表单有提交  
        //检测表单中需要的值  
        if(empty($_POST[&#39;name&#39;])){  
            die("请输入用户名!");  
        }  
        if(empty($_POST[&#39;pass&#39;])){  
            die("请输入密码!");  
        }  
        //设置数据库变量  
        $host = "localhost";  
        $user = "root";  
        $pass = "zq19890319";  
        $db = "cookie";  
        //打开连接  
        $connection = mysql_connect($host, $user, $pass) or die("Unable to connect!");  
        //选择一个数据库  
        mysql_select_db($db) or die("Unable to select database!");  
        //建立一个查询  
        $query = "SELECT * FROM users WHERE name = &#39;".$_POST[&#39;name&#39;]."&#39; AND pass = SHA1(&#39;".$_POST[&#39;pass&#39;]."&#39;)";  
        //执行一个查询  
        $result = mysql_query($query) or die("Error in query:$query." . mysql_error());  
        //是否有记录集返回  
        if(mysql_num_rows($result) == 1){  
            //如果有一行记录返回  
            //表示验证已经通过  
            //建立一个session,设置一个登陆标记为1,并将当前用户名保存在cookie中  
            session_start();  
            $_SESSION[&#39;auth&#39;] = 1;  
            setcookie("username", $_POST[&#39;name&#39;], time()+(84600*30));  
            echo "用户访问已经授权!";  
        }else{  
            echo "错误的用户名或密码!";  
        }  
        //释放记录集  
        mysql_free_result($result);  
        //关闭数据库  
        mysql_close($connection);  
    }  
    else{  
        //如果没有表单提交,则显示一个HTML表单  
    ?>  
    <html>  
    <head></head>  
    <body>  
        <center>  
            <form method="post" action="">  
            用户名<input type="text" name="name" value="<?php echo $_COOKIE[&#39;username&#39;];?>" />  
            <p />  
            密码<input type="password" name="password" />  
            <p />  
            <input type="submit" name="submit" value="登陆" />  
            </form>  
        </center>  
    </body>  
<?php  
    }  
?>
Copy after login


Related recommendations:

ThinkPhp RBAC experience_PHP tutorial

PHP learning to write the perpetual calendar

PHP learning CURL crawler example

The above is the detailed content of Cookie control access authorization for beginners learning PHP. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Tips for playing the Mist Lock Kingdom to give newbies a guide Tips for playing the Mist Lock Kingdom to give newbies a guide Jan 28, 2024 pm 03:33 PM

Mistlock Kingdom is an open world game where players can play as Sons of Fire to survive and explore. The game combines the unique entertainment of action RPG challenges, bringing players endless surprises and joy. In the game, players can explore resources, environments, weapons and more. Some novice players may be curious about how to get started with the game. In this introduction and sharing, we will provide you with some relevant getting started guides. Tips for Beginners to the Fog Lock Kingdom: The danger levels of areas shrouded by miasma are different. During the exploration process, new areas of the map will be gradually unlocked, and the location of the areas shrouded by miasma can be seen. The map will be distinguished by two colors. The blue area can be entered in a short time. The time you can stay will also be different depending on the character's ability level.

Where are the cookies on your computer? Where are the cookies on your computer? Dec 22, 2023 pm 03:46 PM

Cookies on your computer are stored in specific locations on your browser, depending on the browser and operating system used: 1. Google Chrome, stored in C:\Users\YourUsername\AppData\Local\Google\Chrome\User Data\Default \Cookies etc.

Where are cookies stored? Where are cookies stored? Dec 20, 2023 pm 03:07 PM

Cookies are usually stored in the cookie folder of the browser. Cookie files in the browser are usually stored in binary or SQLite format. If you open the cookie file directly, you may see some garbled or unreadable content, so it is best to use Use the cookie management interface provided by your browser to view and manage cookies.

Anchor Point Advent Novice Ten Company Character Recommendations Anchor Point Advent Novice Ten Company Character Recommendations Feb 20, 2024 pm 02:30 PM

Anchor Arrival is a 3D turn-based card game with a high-definition beautiful girl two-dimensional theme. It provides a rich and exciting combination of characters for players to explore and experience. It has many powerful combinations of high-quality lineups. New players are also curious novices. What powerful characters are recommended in the pool? Let’s take a look at the selection reference for novices to win ten consecutive golds! Anchor Point Advent is a powerful character recommendation for novice pools. The first ten-consecutive pick is Alice. She is mainly a single-target lightning-type burst character. The output is very explosive, and the experience will be very friendly to newcomers, so it is highly recommended to choose it. It is recommended to choose the combination of "Alice" + "Antelope" for 10 points. Alice is the most worthy character to output the goldpire attribute, and is not even a bit stronger than the other two characters in the novice card pool. Alice can pass special

Learning to use batch indentation is a skill that PyCharm newbies must master Learning to use batch indentation is a skill that PyCharm newbies must master Dec 30, 2023 pm 12:58 PM

Essential skills for newbies to PyCharm: Mastering the use of batch indentation requires specific code examples Overview: PyCharm is a powerful Python integrated development environment (IDE) that provides many practical tools and functions to help developers improve efficiency . In the daily coding process, we often need to indent the code to keep the code format neat and beautiful. The batch indentation function provided by PyCharm can help us quickly batch indent the code and improve coding efficiency. This article will explore Py

Ancient Crown Beginner's Guide and Gameplay Introduction Ancient Crown Beginner's Guide and Gameplay Introduction Feb 20, 2024 am 11:20 AM

Crown of the Ancients is a high-quality and strategic card mobile game based on Western magical adventure. In-game secret exploration, ruins adventure, national championship and other special gameplay are waiting for you to experience. So for novice players, if they want to get started with this game quickly, a novice guide is indispensable. Today, the editor will bring you the relevant guide, let’s take a look. An overview of the Ancient Crown beginner’s guide, gameplay and area opening styles: 1. Diamond accumulation flow: Everything is focused on accumulating diamonds, and then you start to work hard after leaving the village. Except for the main magic weapon, three flywheel activities, etc., which require diamonds, the others are ignored. The main focus is on one of them. Don't pay attention to the hero challenge. You can fight as many as you can, and don't force it. Advantages: You only need to mess around to accumulate diamonds. After leaving the village, you can quickly access the latest activities with diamonds, get new series of hardware heroes, and diamonds are broken.

Where are the mobile cookies? Where are the mobile cookies? Dec 22, 2023 pm 03:40 PM

Cookies on the mobile phone are stored in the browser application of the mobile device: 1. On iOS devices, Cookies are stored in Settings -> Safari -> Advanced -> Website Data of the Safari browser; 2. On Android devices, Cookies Stored in Settings -> Site settings -> Cookies of Chrome browser, etc.

What are the dangers of cookie leakage? What are the dangers of cookie leakage? Sep 20, 2023 pm 05:53 PM

The dangers of cookie leakage include theft of personal identity information, tracking of personal online behavior, and account theft. Detailed introduction: 1. Personal identity information is stolen, such as name, email address, phone number, etc. This information may be used by criminals to carry out identity theft, fraud and other illegal activities; 2. Personal online behavior is tracked and analyzed through cookies With the data in the account, criminals can learn about the user's browsing history, shopping preferences, hobbies, etc.; 3. The account is stolen, bypassing login verification, directly accessing the user's account, etc.

See all articles