php 通过Session检查用户是否已经登录的简单示例

前端之家收集整理的这篇文章主要介绍了php 通过Session检查用户是否已经登录的简单示例前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
PHP通过Session判断检查用户是否已经登录代码,感兴趣的小伙伴,下面一起跟随编程之家 jb51.cc的小编来看看吧。
functions.PHP,经测试代码如下:

/**
 * 通过Session判断检查用户是否已经登录
 *
 * @param 
 * @arrange (512.笔记) jb51.cc
 **/
function loggedIn(){ 
//Session logged is set if the user is logged in 
//set it on 1 if the user has successfully logged in 
//if it wasn't set create a login form 
if(!$_SESSION['loggd']){ 
echo'<form action="checkLogin.PHP" method="post"> 
<p> 
Username:<br> 
<input type="text" name="username"> 
</p> 
<p> 
Password:<br> 
<input type="password" name="username"> 
</p> 
<p> 
<input type="submit" name="submit" value="Log In"> 
</p> 
</form>'; 
//if session is equal to 1,display 
//Welcome,and whaterver their user name is 
}else{ 
echo 'Welcome,'.$_SESSION['username']; 
} 
} 



/***   来自编程之家 jb51.cc(jb51.cc)   ***/
index.PHP 经测试代码如下:

<?PHP 
//Start the session 
session_start();
//This is a simplified HTML Document 
?> 
<html>
<head>
<title>My Page</title>
</head>
<body>
<?PHP 
//Call the functions file 
require_once("functions.PHP"); 
//Display either the user's name,or the login form 
//This can be placed on many pages without having 
//to re-write the form everytime,just use this function 
logedIn(); 
?> 
</body>
</html>


/***   来自编程之家 jb51.cc(jb51.cc)   ***/
原文链接:https://www.f2er.com/php/528990.html

猜你在找的PHP相关文章