0

我创建了一个会话,但在“login.php”中输入电子邮件和密码后,页面再次重定向到“login.php”。以下代码是检查电子邮件和密码验证的“login.php”的下一页。

<?php
    $con = mysql_connect("localhost","Rahul","");

    if (!$con) {
        die('Could not connect: ' . mysql_error());
    }

    mysql_select_db("ebusiness", $con);
    $result=mysql_query("select * from members where Email='$_POST[login]' and Password='$_POST[pwd]' ") or die(mysql_error());

    while($row=mysql_fetch_array($result)) {
        if(strcmp("trim($_POST[login])","trim($row[Email])"==0 ) && strcmp("trim($_POST[pwd])","trim($row[Password])")==0) {
            echo "<meta http-equiv='refresh' content='0.5;url=home.php'/>";
            session_start();
            $_SESSION['email']=$_POST['login'];
            $_SESSION['id']=$row['id'];
            echo "<br><br><br>";
            echo "<table align='center' border='0' cellspacing='0' cellpadding='0' bgcolor='#F0F0F0'><tr></tr><tr align='center'><td><h3>Please wait...</td></tr></table>";
            return;
        }
    }
    mysql_close($con);
?>

“home.php”页面是在成功验证后显示信息的页面。home.php 的代码:

 <?php
   session_start();

    if (!isset($_SESSION['email'])) {
        header("Location: login.php");
        return;
   } else {
        $con = mysql_connect("localhost","Rahul","");
        mysql_select_db("ebusiness", $con);
        $result = mysql_query("SELECT * FROM members WHERE id = $_SESSION[id]") or die(mysql_error());

        while($row = mysql_fetch_array($result)) {
            echo "<table height=100% width=100% border='0' cellspacing='0'><tr height=20%><td colspan=2>";
            include 'header.php';
            echo "</td></tr>";
        }
   }
?>

会话首先在“welcome.php”上创建,然后在“home.php”上检查会话是否存在。然后它重定向到“login.php”。但是因为我已经创建了一个会话.. 我不明白为什么 home.php 没有加载。请帮助我..

4

3 回答 3

0

session_start();在 login.php 页面中丢失了,将其添加到页面顶部。

于 2013-02-21T07:07:31.713 回答
0

尝试

登录.php

<?php
    ob_start();
    session_start();
    $con = mysql_connect("localhost","Rahul","")or die(mysql_error());
    mysql_select_db("ebusiness", $con)or die(mysql_error());;

    $result = mysql_query("select * from members where Email = '".$_POST['login']."' and Password = '".$_POST['pwd']."' LIMIT 1") or die(mysql_error());

    if(mysql_num_rows($result) > 0)
    {
        while($row = mysql_fetch_array($result))
        {
            echo "<meta http-equiv='refresh' content='0.5;url=home.php'/>";
            $_SESSION['email'] = $_POST['login'];
            $_SESSION['id'] = $row['id'];
            echo "<br /><br /><br />";
            echo "<table align='center' border='0' cellspacing='0' cellpadding='0' bgcolor='#F0F0F0'><tr></tr><tr align='center'><td><h3>Please wait...</td></tr></table>";
            return;
        }
    }

    mysql_close($con);
    flush();
?>
于 2013-02-21T06:56:34.483 回答
0

session_start();

每页的顶部。但是,您在下面看到的并不是您问题的答案。检查这一行:

if(strcmp("trim($_POST[login])","trim($row[Email])"==0 ) && strcmp("trim($_POST[pwd])","trim($row[Password])")==0)
{

看看strcmp("trim($_POST[login])","trim($row[Email])"==0 ) 这意味着

strcmp("sometext","someothertext"==0 ) 

意思是

strcmp("sometext",true)

所以你的线

if(strcmp("trim($_POST[login])","trim($row[Email])"==0 ) && strcmp("trim($_POST[pwd])","trim($row[Password])")==0)
    {

if(true && strcmp("evenmoretext","evenevenevenmoremoretext")==0)
    {

这是

if(true && 0==0)

这是

if(true && true)

所以你达到这个echo "<meta http-equiv='refresh' content='0.5;url=home.php'/>";是因为When a string is specified in double quotes or with heredoc, 变量 are parsed within it.但是没有函数(在这种情况下是修剪)被解析!

一些测试:

$_POST['login'] = 'postemail';
$_POST['pwd'] = 'posttest';
$row['Email'] = 'postemail';
$row['Password'] = 'posttest';

var_dump("trim($_POST[login])"); // string(15) "trim(postemail)"
var_dump("trim($row[Email])"); // string(15) "trim(postemail)"
var_dump("trim($row[Email])"==0); // bool(true)
var_dump(strcmp("trim($_POST[login])","trim($row[Email])"==0 )); //int(1)

在我看来,您应该清除并修复您的代码。例如,第一个 if 语句应该是:

if(strcmp(trim($_POST["login]"),trim($row["Email"]))==0 && strcmp(trim($_POST["pwd]"),trim($row["Password"]))==0)
于 2013-02-21T06:57:08.060 回答