Home > Article > Daily Programming > PHP cookie implementation method to determine whether the user is logged in (image, text + video)
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['username']; echo $_COOKIE['username']; if ($name) { echo "<script> alert(\"尊敬的$name ,欢迎回来!!\"); </script>"; }else{ echo "<script> alert('您还尚未登录!请返回登录~~') </script>"; echo "<a href='login.html'>如果跳转失败请点击跳转~~</a>"; header("Refresh:1;url=login.html"); }
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:
After we have logged in successfully, close and refresh the home page again. The following effect will be displayed:
And when we delete the cookie (inspect the element through the f12 shortcut key), it is equivalent to logging out:
After refreshing the browser, you will get the result as shown below:
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!