简体   繁体   English

php会话不起作用

[英]php session doesn't work

How it should work: Index.php is the secured page. 它应该如何工作:Index.php是安全页面。 It includes check.php, which checks if you have a session = good. 它包括check.php,它检查你是否有一个session = good。 If it hasn't, you're not logged in -> log off, remove session. 如果没有,则表示您尚未登录 - >注销,删除会话。 But it doesn't work, it always logs off, like I didn't log in... 但它不起作用,它总是注销,就像我没有登录...

index.php 的index.php

include ‘check.php’;
echo "logged in";

check.php check.php

session_start();
if($_SESSION[‘login’] != ‘good’) {
unset($_SESSION[‘login’]);
unset($_SESSION[‘name’]);
header(‘Location: login.php?logoff’);
exit();
} 

Login.php 的login.php

if(isset($_POST[‘login’])) {
$gb = array();
$gb[‘user1’] = ‘pass1’;
$gb[‘user2’] = ‘pass2’;
if(isset($gb[$_POST[‘username’]]) && $gb[$_POST[‘username’]] == $_POST[‘password’])
{ 
$_SESSION[‘login’] = ‘good’;
$_SESSION[‘name’] = $_POST[‘name’];

header("Location: index.php");
} else {

header("Location: login.php?wrongpass");

}

} else { ?>
Login Form
<?php } ?>

I hope someone can help me! 我希望有一个人可以帮助我!

您应该验证是否在login.php中启动了会话。

Put session_start(); session_start(); in all the pages 在所有页面中

You need to have session_start() at the top of all the pages, you havent shown the session start for your login page. 您需要将session_start()放在所有页面的顶部,您还没有显示登录页面的会话开始。

(Thanks to Danny for proving I cant type) (感谢Danny证明我不能打字)

检查你的php.ini中是否有register_globals是On

First check on the pages you want to use session variables session is start or not and if session is not stat then start it. 首先检查您要使用的页面会话变量会话是否开始,如果会话不是stat,则启动它。

and this is the very first line in the php file. 这是php文件中的第一行。

Code for the session checking is : 会话检查的代码是:

if(!session_id())
{
    session_start();
}
if($count==1){
    session_start();    
    $_SESSION['Username'] = $UserName;
    $_SESSION['Password'] = $password;
    UpdateOnlineChecker($Session);
    header( "Location: http://". strip_tags( $_SERVER ['HTTP_HOST'] ) ."/newHolo/" );
    exit;
}
else {
    echo "Wrong Username or Password";
}

Look at my code. 看看我的代码。 It checks if the statement is true (for me, if there is one row with a query statement i execute). 它检查语句是否为真(对我来说,如果有一行带有我执行的查询语句)。 Then i start a session and basically Ill define global session variables, sned out a query to my database to update the session and then refer through. 然后我开始一个会话,基本上我定义全局会话变量,发出一个查询到我的数据库,以更新会话,然后通过引用。

you are missing a session_start(); 你错过了session_start(); in your if true block. 在你的if块中。

Use one for action document such as index.php there is code: 使用一个用于动作文档,如index.php,有代码:

session_start();
if(isset($_POST['login']) && isset($_POST['password'])){
   // login
   header('Location: (here is some page)');
}
if(!isset($_SESSION['user']){
  // @todo some action
} else {
  require_once('login.php');
}

if(isset($_GET['logout'])){
   unset($_SESSION['user']);
   header('Location: (here is some page)');
}

I think problem is header: 我认为问题是标题:

('location:------.php);

Your hosting server doesn't run this. 您的托管服务器不运行此。 You can use this: 你可以用这个:

echo "<script>window.location.href='-----.php'</script>";

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

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