简体   繁体   English

PHP会话未通过

[英]PHP Session not being passed

I am working on authentication for my schools username and password. 我正在为学校的用户名和密码进行身份验证。 The school has its own backend script to use for form POST. 学校有自己的后端脚本,可用于POST表格。 It allows some hidden inputs with specific names for customization, for example, page redirecting on success. 它允许某些具有特定名称的隐藏输入进行自定义,例如,成功重定向页面。 However, I can't have any session_start() or any of my php code on that page because after the login.php , the login page gets redirected to the schools page and then gets redirected back to my index.php . 但是,该页面上没有任何session_start()或任何php代码,因为在login.php ,登录页面被重定向到学校页面,然后重定向回我的index.php The post value gets passed through to the index.php but on page refresh, it goes back to the login page because the session is not saved with the post value. 帖子值将传递到index.php但是在页面刷新时,它会返回登录页面,因为会话没有与帖子值一起保存。

Any help on this will very helpful. 在这方面的任何帮助将非常有帮助。

Index.php Index.php

<?php
session_start();
$_SESSION['username'] = $_POST['sid'];
if(!isset($_SESSION['username'])){
header("Location: login.php");
} else {
?>
// HTML here
<?php
}
?>

Login.php Login.php

<form action="authenticator.pl" name="form1" method="POST">
    // Using hidden inputs I specify which page to redirect to... These are specific hidden inputs that are accepted.
</form>

如果您没有将发布数据发送到index.php页面,则以下行将清除用户名会话值。

$_SESSION['username'] = $_POST['sid'];

You have to include the sessions_start(); 您必须包含sessions_start(); at the top of every file where you need to check if they are logged in. 在您需要检查是否已登录的每个文件的顶部。

Put an if statement at the top of login.php to check if they're session exists. 在login.php的顶部放置一个if语句,以检查它们是否存在会话。 If it does then redirect the to index.php. 如果确实如此,则将其重定向到index.php。

At the top of index.php check if they are logged in using an if statement. 在index.php的顶部检查是否使用if语句登录。 If they are then proceed to rest of index.php if not (else {}) then redirect them to login.php 如果不是,则继续进行index.php的其余部分(否则{}),然后将其重定向到login.php

Set the SESSION after you run your checks and authentications in your login.php script. 在您的login.php脚本中运行检查和身份验证之后,设置SESSION。 You can set a session like this: 您可以像这样设置会话:

$_SESSION['userid']     = $id;
$_SESSION['username']   = $name;
$_SESSION['userpass']   = $pass;

Just be sure to add session_start(); 只要确保添加session_start(); at the top of index.php and check if they're sessions equate to a logged in user. 在index.php的顶部,并检查它们是否等于已登录用户的会话。

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

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