简体   繁体   English

php 会话的帮助

[英]Help with php sessions

I like to know how to use a condition on php sessions我想知道如何在 php 会话中使用条件

in this code if the user is not loged in page will redirect to login.php.在此代码中,如果用户未登录页面将重定向到 login.php。

<?
session_start();
if(!session_is_registered(username)){
header("location: login.php");
}
?> 

what i want is to redirect user to another php if the user is loged in. if not stay on the same page.如果用户已登录,我想要将用户重定向到另一个 php。如果不留在同一页面上。 like if user is not loged in keep the user in index page and if user is loged in redirect the user to user.php例如,如果用户未登录,则将用户保留在索引页面中,如果用户已登录,则将用户重定向到 user.php

for the login script im using a code fount in this site:http://www.phpeasystep.com/phptu/6.html对于登录脚本我使用此站点中的代码源:http://www.phpeasystep.com/phptu/6.html

thanks in advance.提前致谢。

Set a variable in $_SESSION when you have logged in.登录后在 $_SESSION 中设置一个变量。

ie in login.php:即在 login.php 中:

if ( $passWordCorrect ) {
 session_start();
 $_SESSION['loggedIn'] = true;
}

in index.php:在索引中。php:

session_start();
if ( !empty( $_SESSION['loggedIn'] ) ) {
 // User logged in; do magic.
} else {
 header('Location: user.php');
}
<?
session_start();
if(!$_SESSION['username]){
header("location: login.php");
}
?> 

And in login page you asign the variable like this:在登录页面中,您可以像这样分配变量:

<?php
session_start();
$_SESSION['username']='JohnDoe';
?>

The code is on the same page as the tutorial you linked to:该代码与您链接到的教程位于同一页面上:

<?php 
session_start();
if(!session_is_registered(myusername)){
header("location:main_login.php");
}
?>

But really you should be using the $_SESSION variable.但实际上你应该使用$_SESSION变量。 On the login page:在登录页面上:

<?php
session_start()
$_SESSION['username'] = $username;
?>

And then on the other pages:然后在其他页面上:

<?php
session_start()
if (!isset($_SESSION['username'])) {
    header('location: login.php')
}
?>

UPDATE更新

It is better to not use short tags (ie <?php instead of ?> )最好不要使用短标签(即<?php而不是?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM