php登录脚本不重定向

时间:2013-12-08 08:02:05

标签: php redirect login

我写了一个正确读取用户和密码数据的脚本,以登录到保留的网页。问题是,当您按下登录按钮时,它不会重定向到target.php(保留)页面。表单位于名为login.php的文件中。 它读取并连接到数据库,但保留在此登录页面上:

<?php


session_start();


function loginform(){

    echo "<form action='' method='POST'>
          Username: <input type = 'text' name='username'>
          Password: <input type = 'text' name='password'>
          <input type = 'submit' name='login' value='Login'>
          </form>

    ";
}

function logoutform(){

    echo "<form action='' method='POST'>
          <input type = 'submit' name='logout' value='Logout'>
          </form>

    ";

}

function login($username, $password){

    $pass = md5($password);

    $con= mysqli(whateverwebsite.com, 'Nameofthetable', 'hashedmd5password') or die (mysql_error());

    mysql_select_db('whateverdb', $con) or die (mysql_error());

    $result = mysql_query(" SELECT * FROM user WHERE username='$username' AND password='$pass' ") or die (mysql_error);

    $count= mysql_num_rows($result);

    if($count==1) {
        $_SESSION['login']=$username;
        header('Location:target.php'); /* this does not work */
    }

    else {
        header('Location:index.php');   /* this does not work */
        echo "Wrong login";
    }

}

function logout(){
    session_destroy();
}

if (isset($_SESSION['login'])) {
    echo "You've logged in";
    logoutform();
}

else{
    echo "Enter with Username and password.";
    loginform();
}

if ($_POST['login']) {
    echo "logging in..."; /* this text "logging in" remains on the screen, instead of going to target.php */

    login($_POST['username'], $_POST['password']);
}

elseif($_POST['logout']){
    echo "Logging out";
    logout();
}

?> 

另外,在target.php页面的html之前,有这个

<?php

 session_start();
 echo "Reserved area<br>";

 if (!isset($_SESSION['login'])) {
    exit("you must login <a href='../login.php'>Login<a>");
 }
 else {
    echo "Do the <a href='../login.php'>Logout</a>";
 }

?>

6 个答案:

答案 0 :(得分:1)

这样的事总是对我有用:
这个修改在你的login函数中。如果有效,您可以用类似的方式修改else部分。

if( $count == 1 )
{
    $_SESSION['login'] = $username;

    echo
'<!DOCTYPE html>
<html>
    <head>
        <title>Your website title</title>
        <meta http-equiv="refresh" content="3;url=target.php">
        <meta charset="UTF-8">
    </head>
    <body>
        Logging in . Please wait ...
    </body>
</html>';

}

答案 1 :(得分:0)

您可以使用header()函数发送新的HTTP标头,但必须在任何HTML或文本之前将其发送到浏览器(例如,在声明之前)。

替代:

function Redirect($url, $permanent = false)
{
    header('Location: ' . $url, true, $permanent ? 301 : 302);

    exit();
}

Redirect('http://www.google.com/', false);

<强>更新 如果这不起作用,请使用:

echo "<meta http-equiv='refresh' content='0;url=index.php' />";

而不是使用标题

答案 2 :(得分:0)

在使用header()之前,你不应回应任何事情。

因此,首先检查$_POST['login'],然后在login()之前不要使用回音

答案 3 :(得分:0)

另一种方法:

<?php
    echo "<script type='text/javascript'>window.location='page.php';</script>";
    //or
    echo "<meta http-equiv='refresh' content='0;url=page.php' />";
?>

答案 4 :(得分:0)

//在这里试试

 if($count==1) {
        $_SESSION['login']=$username;
        header('Location:target.php'); /* this will work now*/
        //TRY
         exit(); 
    }

注意:不推荐使用mysql_ *使用mysqli_您的代码非常容易受到攻击

逻辑缺陷 - 您的代码非常容易受到攻击

  $result = mysql_query(" SELECT * FROM user WHERE username='$username' AND    password='$pass' ") or die (mysql_error);
  $count= mysql_num_rows($result);

$username="A%"像某些东西一样容易获得$ count&gt; 1并登录此

答案 5 :(得分:0)

我认为这个功能可能是罪魁祸首。它没有显示任何内容,为什么它不会被调用?

if (isset($_SESSION['login'])) {
    echo "You've logged in";
    logoutform();
}