duanfei8897 2013-11-06 21:29
浏览 39
已采纳

用php更改页面

I am trying to change the page when the user hits the login button. When the login button is hit currently the page just refreshed the user is not redirected to the new page. I created the session before any of the code for the page. I am wondering if it has to do with the location of my header command.

    <?php 
    session_start(); 
    ?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <title>Login</title>
    </head>

    <body>
        <form action="" method="post">
            <br>Name: <input type="text" name="NameTextBox"><br>
            <br>What grade are you currently in?: <input type="number" name="GradeTextBox"><br>
            <br><button name="Login" type="submit">Login</button></br>
        </form>

        <?php

            if (isset($_POST["Login"])) {
                $_SESSION["start"] = $_SERVER['REQUEST_TIME'];
                //echo "This session is beginning at ".$_SESSION["start"]."<br /><br />";
            }   

            if(isset($_POST["Login"])){
                if($NameTextBox = "Phydeaux"){
                    //echo"Good Name!";
                     PassLogin();
                }
                elseif($NameTextBox = "Rover"){
                    //echo"Good Name!"; 
                     PassLogin();
                }
                elseif($NameTextBox = "Spot"){
                    //echo"Good Name!";
                    PassLogin();                                         
                }
                else{
                    echo"You cannot login!";
                }

            }

            function PassLogin()
            {
                //print '<script type="text/javascript">'; 
                //print 'alert("Running PassLogin Function")'; 
                //print '</script>';  

                $_SESSION["ReadingGrade"] = "Fail";
                $_SESSION["WritingGrade"] = "Fail";
                $_SESSION["MathGrade"] = "Fail";
                $_SESSION["Grade"] = $GradeTextBox;
                $_SESSION["Name"] = $NameTextBox;
                Header('Location: Reading.php');
            }

            if (isset($_POST["Login"])){
                //echo "Login has been pressed";
            }

        ?>  

    </body>
</html>
  • 写回答

3条回答 默认 最新

  • dow57588 2013-11-06 22:04
    关注

    There are several synthax and technical problems with this code. In addition to what AlexP said, you must observe that you cannot send the headers when you have already sent some output. The headers must be sent before any output, what is the same reason you had to put your session_start() in the beginning.

    That being said, you should also make the login script before the HTML part, where your session_start() is, so your redirect header to Reading.php can work too. Or you can workaround that by placing a ob_start() in the beginning of the page, it will buffer the output preventing it from being sent before the script ends, this way you can call a header() wherever you like.

    And talking about the header, the function is header(), not Header(). There is no native function named Header() in PHP. Function names are case sensitive.

    In addition, you shouldn't use action="" in your <form>. Just suppress this attribute to make it post to the same page, it will make your code work don't matter what your php file name is. If you use action="" it will post to the index.php instead of login.php.

    I also noticed you are using Reading.php (with capital letter) and login.php. I suggest you normalize all your file names to lower case, because if not you can have problems when porting this code to other systems. To Windows, Reading, reading, READING and ReAdInG are the same thing, to Linux it is not.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 CSS实现渐隐虚线边框
  • ¥15 thinkphp6配合social login单点登录问题
  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题