PHP cookie implementation method to determine whether the user is logged in (image, text + video)

藏色散人
Release: 2018-10-18 15:59:14
Original
5087 people have browsed it

This article mainly introduces PHP cookie implementation method to determine whether the user is logged in.

Basic knowledge points related to cookies in PHP, we have discussed in the previous article [How to record and delete variables in cookies in PHP? ] and [PHP cookie implementation method of recording user login information] have been introduced in detail for everyone.

Then the content of this article will be introduced based on the previous cookie-related knowledge points. Friends in need can choose to refer to it.

Below we will use a simple example to introduce the PHP cookie implementation method to determine whether the user is logged in.

<?php
echo "<h1>这里是主页</h1>";
@$name = $_COOKIE[&#39;username&#39;];
echo $_COOKIE[&#39;username&#39;];
if ($name) {
    echo "<script>
     alert(\"尊敬的$name ,欢迎回来!!\");
</script>";
}else{
    echo "<script>
    alert(&#39;您还尚未登录!请返回登录~~&#39;)
</script>";
    echo "<a href=&#39;login.html&#39;>如果跳转失败请点击跳转~~</a>";
    header("Refresh:1;url=login.html");
}
Copy after login

Here we simply use js to simply write a prompt pop-up window to determine whether the user is logged in.

When we log in for the first time, the result after successfully jumping to the main page is as follows:

PHP cookie implementation method to determine whether the user is logged in (image, text + video)

After we have logged in successfully, close and refresh the home page again. The following effect will be displayed:

PHP cookie implementation method to determine whether the user is logged in (image, text + video)

And when we delete the cookie (inspect the element through the f12 shortcut key), it is equivalent to logging out:

PHP cookie implementation method to determine whether the user is logged in (image, text + video)

After refreshing the browser, you will get the result as shown below:

PHP cookie implementation method to determine whether the user is logged in (image, text + video)

Re-login will be displayed here.

This article is an introduction to the method of using PHP cookie to determine whether the user is logged in. It is also very simple. I hope it will be helpful to friends in need!

If you want to know more about PHP, you can follow the PHP Chinese website PHP Video Tutorial, everyone is welcome to refer to and learn!

The above is the detailed content of PHP cookie implementation method to determine whether the user is logged in (image, text + video). For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!