标题不能与会话一起使用 [英] Header Not Working With Sessions

查看:94
本文介绍了标题不能与会话一起使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在php中使用会话和cookie创建了一个登录系统。



登录后,我希望用户按顺序浏览页面,防止它们返回通过使用后退按钮或输入网址(甚至当他们登录时)到上一页或任何页面。除了第一个信息页面,每个页面都有一个问题,只有正确的答案才能让你进入下一页,就像寻宝。为此,我为每个页面使用会话变量。



登录后的第一页(users-area.php):

 <?php 
session_start();
require_once(user.cookies.php);

$ firstname_user = $ _SESSION [SESS_USERFIRSTNAME];
?>

/ * html部分* /
< a href =q1.php>问题1< / a>< br>< br>
<?php
$ _SESSION [from_start] =true;
?>

< a href =logout.php>注销< / a>

之后的第二页(q1.php):

 <?php 
session_start();
require_once(user.cookies.php);

$ firstname_user = $ _SESSION [SESS_USERFIRSTNAME];

if(isset($ _ POST [ans1])){//如果提交了答案
if($ _POST [ans1] ==correct){//如果答案正确
$ _SESSION [from_q1] =true; //用于验证下一页,q2.php
header('Location:q2.php'); //前往下一页
}
else {//如果答案不正确
$ _SESSION [from_start] =true; //相同的页面加载
}



if($ _ SESSION ['from_start'] ==false){//检查它是否from users-area.php
// if not,logout
header(Location:logout.php);
}
else {
//如果是的话,重置变量以使它们不能从其他地方访问
$ _SESSION ['from_start'] =false;
}
?>

// html部分
< form method =POST> //发送输入到同一页面
< input type =textname =ans1>
< input name =submittype =submitvalue =Sumbit>
< / form>
< a href =logout.php>注销< / a>

之后的页面(q2.php):

 <?php 
session_start();
require_once(user.cookies.php);

$ firstname_user = $ _SESSION [SESS_USERFIRSTNAME];
$ b $ if($ _ SESSION ['from_q1'] ==false){
header(Location:logout.php);
}
else {
$ _SESSION ['from_q1'] =false;
}

}
?>

登录系统效果很好。但是,在添加所有这些限制后,在q1.php中,提交正确答案会导致注销。即使我已经在与注销相关的任何内容之前放置了标头('Location:q2.php'); >。提交正确的答案应该会导致q2.php。



其他一切正常。 q1.php中的错误答案只按预期重新加载页面。如果有帮助,这里是user.cookies.php:

 <?php 
//重定向函数
函数returnheader($ location){
$ returnheader = header location:$ location);
返回$ returnheader;


if(!strlen($ _ SESSION [SESS_USERNAME])){
// redirect
returnheader(index.php);
}
?>

为什么q1.php不能正确使用正确答案?它是不是一个接一个地被使用?

解决方案

标题('Location:q2.php');您必须退出脚本,否则执行将继续执行。

  if(isset($ _ POST [ans1]] )){//如果答案提交
if($ _POST [ans1] ==correct){//如果答案正确
$ _SESSION [from_q1] =true ; //用于验证下一页,q2.php
header('Location:q2.php'); //前往下一页
exit;
}
else {//如果答案不正确
$ _SESSION [from_start] =true; //同一页面加载
}

}


I have made a login system using sessions and cookies in php.

After logging in, I want users to go through pages in order, preventing them to go back to previous page or any page by using back button or typing url(Even when they are logged in). Except for the first information page, every page has a question and only the correct answer can get you to next page, sort of like a treasure hunt. For that I am using session variables for every page.

First page after login (users-area.php):

<?php
session_start();
require_once("user.cookies.php");

$firstname_user = $_SESSION["SESS_USERFIRSTNAME"];
?>

/*html part*/
<a href="q1.php">Question 1</a><br><br>
<?php
    $_SESSION["from_start"] = "true";
?>

<a href="logout.php">Logout</a>

Second Page after that (q1.php):

<?php
session_start();
require_once("user.cookies.php");

$firstname_user = $_SESSION["SESS_USERFIRSTNAME"];

if (isset($_POST["ans1"])) {         //if answer is submitted
    if ($_POST["ans1"]=="correct") {          //if answer is correct
        $_SESSION["from_q1"] = "true";    //for verifying in next page, q2.php
        header('Location: q2.php');       //heading to next page
    }
    else{                                 //if answer is not correct
        $_SESSION["from_start"] = "true";   //same page loads   
    }

}

if($_SESSION['from_start'] == "false"){    //to check that it came from users-area.php
   //if not, logout
   header("Location: logout.php");
}
else{             
   //if yes,reset the variable so that they can't access from anywhere else anymore
   $_SESSION['from_start'] = "false";
}
?>

//html part
<form method="POST">   //sending input to same page
    <input type="text" name="ans1">
    <input name="submit" type="submit" value="Sumbit">
</form>
<a href="logout.php">Logout</a>

The Page After That (q2.php):

<?php
session_start();
require_once("user.cookies.php");

$firstname_user = $_SESSION["SESS_USERFIRSTNAME"];

 if($_SESSION['from_q1'] == "false"){
   header("Location: logout.php");
 }
 else{
   $_SESSION['from_q1'] = "false";
 }

}
?>

The login system works great. But after adding all these restrictions, in q1.php, submitting the right answer leads to logging out. Even though I have put header('Location: q2.php'); before anything related to logout. Submitting the right answer should lead to q2.php.

Everything else works. Wrong answer in q1.php only reloads the page as expected. And trying to go to any page by back button or typing url leads to logout.

If it helps, here is user.cookies.php :

<?php
  //redirect function
  function returnheader($location){
    $returnheader = header("location: $location");
    return $returnheader;
  }

  if(!strlen($_SESSION["SESS_USERNAME"]) ){
    //redirect
    returnheader("index.php");
  }
?>

Why isn't q1.php working correctly with the right answer? Is it that header cannot be used one after the other?

解决方案

After header('Location: q2.php'); you must exit the script or the execution will continue on the next line.

if (isset($_POST["ans1"])) {         //if answer is submitted
    if ($_POST["ans1"]=="correct") {          //if answer is correct
        $_SESSION["from_q1"] = "true";    //for verifying in next page, q2.php
        header('Location: q2.php');       //heading to next page
        exit;
    }
    else{                                 //if answer is not correct
        $_SESSION["from_start"] = "true";   //same page loads   
    }

}

这篇关于标题不能与会话一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆