标头重定向时未设置会话变量

发布于 2024-12-08 07:32:34 字数 1818 浏览 0 评论 0原文

我遇到的问题是我的会话变量未设置/保存。

这是我的代码:

index.php 有:

<? //this is first line of page
session_start(); 
?>
<form action="admin_process_login.php" method="post">
<p>EMAIL</p><input name="email" type="text">
<p>PASSWORD</p><input name="password" type="password" />
<input type="submit" value="Enter">
</form>

admin_process_login.php

<? //this is first line of page
session_start(); 
$useremail = $_POST['email'];
$postpassword = $_POST['password'];
include('admin_config.php');

if ($postpassword != "" && $useremail != "") {
//Connect to database
mysql_connect("localhost", $dbusr, $dbpass) or die(mysql_error());
mysql_select_db("studioel_dental") or die(mysql_error());

//Look for a matching email/password
$query = "SELECT *
    FROM users
    WHERE users.email = '$useremail'
    AND users.password = '$postpassword'";

$result = mysql_query($query) or die(mysql_error());
$row = mysql_fetch_array($result);

if(mysql_num_rows($result)==0){
    header("Location: http://www.dentalbenefitprogram.com/admin.php?error=nomatch");
    } else {
    //set session variables and load supplies page
        $uid = $row['id'];
        $unamex = $row['name'];
        $uemailx = $row['email'];
        $utypex = $row['type'];

        $_SESSION['userid']=$uid;
        $_SESSION['uname']=$unamex;
        $_SESSION['uemail']=$uemailx;
        $_SESSION['utype']=$utypex;

        header("Location: http://www.dentalbenefitprogram.com/admin_groups.php");
        exit;
        };
} else {
//email or password fields were blank. Return to login page
header("Location: http://www.dentalbenefitprogram.com/admin.php?error=blank");
};
?>

您可能会猜到:会话变量没有被设置...

非常感谢任何帮助!

I'm having a problem where my session variables aren't getting set/saved.

Here is my code:

index.php has:

<? //this is first line of page
session_start(); 
?>
<form action="admin_process_login.php" method="post">
<p>EMAIL</p><input name="email" type="text">
<p>PASSWORD</p><input name="password" type="password" />
<input type="submit" value="Enter">
</form>

admin_process_login.php

<? //this is first line of page
session_start(); 
$useremail = $_POST['email'];
$postpassword = $_POST['password'];
include('admin_config.php');

if ($postpassword != "" && $useremail != "") {
//Connect to database
mysql_connect("localhost", $dbusr, $dbpass) or die(mysql_error());
mysql_select_db("studioel_dental") or die(mysql_error());

//Look for a matching email/password
$query = "SELECT *
    FROM users
    WHERE users.email = '$useremail'
    AND users.password = '$postpassword'";

$result = mysql_query($query) or die(mysql_error());
$row = mysql_fetch_array($result);

if(mysql_num_rows($result)==0){
    header("Location: http://www.dentalbenefitprogram.com/admin.php?error=nomatch");
    } else {
    //set session variables and load supplies page
        $uid = $row['id'];
        $unamex = $row['name'];
        $uemailx = $row['email'];
        $utypex = $row['type'];

        $_SESSION['userid']=$uid;
        $_SESSION['uname']=$unamex;
        $_SESSION['uemail']=$uemailx;
        $_SESSION['utype']=$utypex;

        header("Location: http://www.dentalbenefitprogram.com/admin_groups.php");
        exit;
        };
} else {
//email or password fields were blank. Return to login page
header("Location: http://www.dentalbenefitprogram.com/admin.php?error=blank");
};
?>

You can probably guess: the session variables aren't being set...

Any help is appreciated greatly!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

季末如歌 2024-12-15 07:32:34

Brian:

我的猜测是,在第一行的评论之前,有一个空格导致了这个问题。

PHP 手册提醒用户注意这一点:

注意:
要使用基于 cookie 的会话,必须在向浏览器输出任何内容之前调用 session_start()。

此外,您可能需要考虑使用长标签而不是短标签。

最好的

Brian:

My guess is that before your comment in the first line you have a space that is causing this problem.

PHP manual cautions users to watch out for this:

Note:
To use cookie-based sessions, session_start() must be called before outputing anything to the browser.

Also, you may want to consider using long tags instead of short tags.

Best

静水深流 2024-12-15 07:32:34

在代码的最开头使用 ob_start();
&不要忘记在代码的最末端添加 ob_end_flush();

use ob_start(); at the extreme beginning of ur code
& dont forget to add ob_end_flush(); at the extreme end of ur code

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文