当用户登录时,我希望他们有一个注销按钮并重定向到他们所在的页面,但是,还有一些其他功能,这些功能都被调用。不幸的是,按下注销按钮时没有任何反应。
这是logout.php文件的代码。
<input type="submit" type="submit" name="submit" value="Log out">
<?php
if (isset($_POST['submit'])){
session_start();
$_SESSION = array();
if (ini_get("session.use_cookies")) {
$yesterday = time() - (24 * 60 * 60); $params = session_get_cookie_params();
setcookie(session_name(), '', $yesterday,
$params["path"], $params["domain"],
$params["secure"], $params["httponly"] );
}
session_destroy();
header('Location: '.$_SERVER['PHP_SELF']);
}
?>
答案 0 :(得分:0)
session_start()
$_SESSION['uname']
是否为空...如果为空则通过调用session_destroy()
header("/")
// HTML
<a href="logout.php">logout</a>
-OR -
<input type="button" value="Logout" onclick="window.location.href = 'logout.php';">
-OR -
<button onclick="window.location.href = 'logout.php';">Logout</button>
// LOGOUT.PHP
<?php
//continue current session
session_start();
//check to see if session variable (uname) is set
//if set destroy the session
if(!empty($_SESSION['uname'])){
session_destroy();
}
//redirect the user to the home page
header("Location: /");
?>
答案 1 :(得分:0)
只需将其保存为“ login.php ”并从浏览器进行访问即可。 UserId为“用户”,密码为“传递”。
<?php session_start(); ?>
<?php if ( array_key_exists( 'uid', $_SESSION ) ): ?>
<?php /* if $_SESSION['uid'] is set, user is already logged in */ ?>
<?php if ( array_key_exists( 'cmd', $_GET ) && $_GET['cmd']==='logout' ): ?>
<?php
unset( $_SESSION[ 'uid' ] );
unset( $_SESSION[ 'error' ] );
session_destroy();
?>
<h1>See you soon!</h1>
Click
<a href="<?php echo $_SERVER['PHP_SELF']; ?>?r=<?php echo md5(uniqid(rand())); ?>">here</a>
if you don't get redirected automatically within 5 seconds.
<script type="text/javascript">
setTimeout( function() {
location.href = "<?php echo addslashes( $_SERVER['PHP_SELF'] ); ?>?r=<?php echo md5(uniqid(rand())); ?>";
}, 5000 );
</script>
<?php else: ?>
<h1>You are logged in</h1>
<a href="<?php echo $_SERVER['PHP_SELF']; ?>?cmd=logout&r=<?php echo md5(uniqid(rand())); ?>">Log Out</a> |
<a href="<?php echo $_SERVER['PHP_SELF']; ?>?r=<?php echo md5(uniqid(rand())); ?>">Refresh</a>
<?php endif; ?>
<?php else: ?>
<?php /* user is not logged in */ ?>
<?php if ( strtolower($_SERVER['REQUEST_METHOD']) == 'post' ): ?>
<?php
// login (POST) request, let's check if credentials
// are correct
unset( $_SESSION['error'] );
if ( array_key_exists( 'userid', $_POST ) && array_key_exists( 'passwd', $_POST ) )
{
$userid = trim( $_POST['userid'] );
$passwd = trim( $_POST['passwd'] );
if ( $userid === '' )
{
$_SESSION['error'] = 'No userid supplied';
}
elseif ( $passwd === '' )
{
$_SESSION['error'] = 'No password supplied';
}
elseif ( $userid !== 'user' || $passwd !== 'pass' )
{
$_SESSION['error'] = 'Wrong userid or password';
}
else
{
$_SESSION['uid'] = $userid;
// from now on, the user is logged in
}
}
else
{
$_SESSION['error'] = 'Missing userid or password';
}
// redirect the user anyways
// this gets rid of posted data if this was a POST,
// so when user reloads the page it doesn't try to
// re-authenticate
// Can be a different URL if user is logged in
// successfully
header( 'Location: ' . $_SERVER['PHP_SELF'] . '?r=' . md5(uniqid(rand())) );
exit;
?>
<?php else: ?>
<?php /* user not logged in, let's display login form */ ?>
<h1>Log In</h1>
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<label for="user-id">User Id</label>
<input type="text" name="userid" id="user-id" />
<label for="user-pwd">Password</label>
<input type="password" name="passwd" id="user-pwd" />
<input type="submit" value="Log In" />
</form>
<?php /* in case any errors occured, show them */ ?>
<?php if ( array_key_exists( 'error', $_SESSION ) && $_SESSION['error'] ): ?>
<div class="error-msg"><?php echo $_SESSION['error']; ?></div>
<?php endif; ?>
<?php endif; ?>
<?php endif; ?>
在您网站的任何其他(PHP)页面中,您可以通过检查来限制对经过身份验证的用户的访问:
<?php
if ( array_key_exists( 'uid', $_SESSION ) )
{
/* user is logged in, do whatever */
}
else
{
/* user is NOT logged in, do whatever else */
}
?>