doulian4467 2014-04-24 13:01
浏览 19
已采纳

用PHP登录后转到下一页[复制]

This question already has an answer here:

I'm doing my homework that a webpage. I created the index page as a login page that have a username input, password input and a login button.

When I click on the button, a php code (it have wroten below) have executed: it uses MySQL for login.

My problem with the login that the redirect. If the typed username and password are okay, the page should go to the next page (for example the next page's link is http://stackoverflow.com).

I use header("location:http://stackoverflow.com"); to go to the next page, but I have got the following error: Warning: Cannot modify header information - headers already sent by (output started at /users/kzr/www/index.php:60) in /users/kzr/www/index.php on line 14

I've already tried ob_start() with ob_end_flush(), but they didn't changed the situation: the error was same.

Can anybody help me to get work this code?

Note: the PHP and the HTML codes are in once file, but I separated them.

The PHP code:

if(isset($_POST["login"])){
    $username=$_POST["username"];
    $password=$_POST["password"];
    $connect=mysql_connect("127.0.0.1","kzr_zetr","350adagsertesborda");
    if($connect){
        $db=mysql_select_db("kzr_zetr");
        if($db){
            $query=mysql_query("SELECT * FROM login WHERE username = '$username' AND password = '$password'");
            $rowcount=mysql_num_rows($query);
            if($rowcount>0){
                $uri="http://kzr.bplaced.de/session/?username=".$username;
                header('Location:'.$uri);
                print 'sucess';
            }else{
                echo "<script type='text/javascript'>login_f_val_phperr();</script>";
                print 'fail';
            }
        }
    }
}

The HTML code:

<!DOCTYPE html>
<html>
<head>
    <title>Bejelentkezés | ZETR</title>
    <meta charset="utf-8" />
    <meta name="author" content="Kókai Z. Ronald" />
    <meta name="contact" content="zr.kokai@gmail.com" />
    <meta name="description" content="(Konrad) Zuse Elektronikus Tanulmányi Rendszer" />
    <meta name="keywords" content="konrad, zuse, konrad zuse, etr, rendszer, tanulmányi rendszer" />
    <meta name="subject" content="Alkalmazott Informatika Házi Feladat Varga István Tanár Úrnak" />
    <base href="http://kzr.bplaced.de" />
    <link rel="stylesheet" type="text/css" href="/f/font/ubuntu/ubuntu.css" />
    <link rel="stylesheet" type="text/css" href="/f/css/zetr.css" />
    <link rel="stylesheet" type="text/css" href="/f/css/login.css" />
    <link rel="shortcut icon" type="image/x-icon" href="/f/img/favicon.ico" />
    <script type="text/javascript" src="/f/script/login-f.js"></script>
</head>
<body>
<div id="container">
    <h1>Zuse</h1>
    <h2>Elektronikus Tanulmányi Rendszer</h2>
    <div id="loginbox">
        <form name="login-f" action="" onsubmit="return login_f_val()" method="post">
            <input type="text" name="username" id="username" placeholder="Felhasználónév" autocomplete="off" />
            <input type="password" name="password" id="password" placeholder="Jelszó" />
            <input type="submit" name="login" id="login" value="Bejelentkezés" />
        </form>
    </div>
</div>
</body>
</html>
</div>
  • 写回答

3条回答 默认 最新

  • duanhanzi8328 2014-04-24 13:11
    关注

    It means on line 60 of index.php something is already causing the headers to be sent; make sure the headers() call is the first thing you do before sending any HTML (or plaintext) output.

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

报告相同问题?

悬赏问题

  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题