简体   繁体   English

php登录页面没有任何反应

[英]php login page nothing happens

I have problem with login page. 我的登录页面有问题。 When I load page first time this happend: 当我第一次加载页面时,发生了这种情况:

http://postimg.org/image/o36250qk5 http://postimg.org/image/o36250qk5

And when I type username and password, wrong or true I got this screen: 当我输入用户名和密码时,输入错误或正确时,出现以下屏幕:

http://postimg.org/image/v53zr7u5x http://postimg.org/image/v53zr7u5x

This is my code: 这是我的代码:

    <!DOCTYPE html>
<html>
<head>
        <title>LOGIN PAGE</title>
    </head>
<body>

<?php
session_start();
    if($_POST) 
    {
        require_once 'config.php';
        $username = $_POST['username'];
        $password = $_POST['password'];     
        $conn = mysql_connect($dbhost,$dbuser,$dbpass)
            or die ('Error connecting to mysql');
        mysql_select_db($dbname);
        $query = sprintf("SELECT COUNT(ID) FROM users WHERE UPPER(username) = UPPER('%s') AND password='%s'",
            mysql_real_escape_string($username),
            mysql_real_escape_string(md5($password)));
        $result = mysql_query($query);
        list($count) = mysql_fetch_row($result);
        if($count == 1) 
        {
            $_SESSION['authenticated'] = true;
            $_SESSION['username'] = $username;
            header('Location:index.php');
        } 
    }
        else {
            session_destroy();
            echo ("Error: that username and password combination does not match any currently within our database");
        }
?>
<form method='post' action='login.php'>
Username: <input type='text' name='username' /><br />
Password: <input type='password' name='password' /><br />
<input type='submit' value='Login' />
</form>
<br>
<a href="index.php">INDEX</a>
</body>
</html>

Please check it and tell me what I done wrong, thanks... 请检查并告诉我我做错了,谢谢。

Try this, Move your else condition inside the if($_POST) 试试这个,在if($_POST)内移动else条件

<?php @ob_start(); 
 @session_start();?>
<!DOCTYPE html>
<html>
<head>
        <title>LOGIN PAGE</title>
 </head>
<body>

<?php
if($_POST) 
{
    require_once 'config.php';
    $username = $_POST['username'];
    $password = $_POST['password'];     
    $conn = mysql_connect($dbhost,$dbuser,$dbpass)
        or die ('Error connecting to mysql');
    mysql_select_db($dbname);
    $query = sprintf("SELECT COUNT(ID) FROM users WHERE UPPER(username) = UPPER('%s') AND password='%s'",
        mysql_real_escape_string($username),
        mysql_real_escape_string(md5($password)));
    $result = mysql_query($query);
    $count = mysql_num_rows($result);
    if($count == 1) 
    {
        $_SESSION['authenticated'] = true;
        $_SESSION['username'] = $username;
        header('Location:index.php');
    } else {
        session_destroy();
        echo ("Error: that username and password combination does not match any currently within our database");
    }
} ?>

<form method='post' action='login.php'>
Username: <input type='text' name='username' /><br />
Password: <input type='password' name='password' /><br />
<input type='submit' value='Login' />
</form>
<br>
<a href="index.php">INDEX</a>
</body>
</html>

NOTE: Use mysqlii_* functions or PDO instead of using mysql_* functions(deprecated) 注意:使用mysqlii_ *函数或PDO代替使用mysql_ *函数(不建议使用)

You have started your output and not initialized session_start() . 您已开始输出,但尚未初始化session_start() This function must be called before any output, so here can be solution. 必须在任何输出之前调用此函数,因此可以作为解决方案。

Your code will look something like this: 您的代码将如下所示:

<?php session_start(); // we start sessions before output ?>
<!DOCTYPE html>
<html>
<head>
        <title>LOGIN PAGE</title>
    </head>
<body>

<?php
    if(isSet($_POST) && $_POST) 
    {
        require_once 'config.php';
        $username = $_POST['username'];
        $password = $_POST['password'];     
        $conn = mysql_connect($dbhost,$dbuser,$dbpass)
            or die ('Error connecting to mysql');
        mysql_select_db($dbname);
        $query = sprintf("SELECT COUNT(ID) FROM users WHERE UPPER(username) = UPPER('%s') AND password='%s'",
            mysql_real_escape_string($username),
            mysql_real_escape_string(md5($password)));
        $result = mysql_query($query);
        list($count) = mysql_fetch_row($result);
        if($count == 1) 
        {
            $_SESSION['authenticated'] = true;
            $_SESSION['username'] = $username;
            header('Location:index.php');
        } 
        else {
            session_destroy();
            echo ("Error: that username and password combination does not match any currently within our database");
        }
    }
?>
<form method='post' action='login.php'>
Username: <input type='text' name='username' /><br />
Password: <input type='password' name='password' /><br />
<input type='submit' value='Login' />
</form>
<br>
<a href="index.php">INDEX</a>
</body>
</html>

Also I edited 我也编辑了

if($_POST)

to

if(isSet($_POST) && $_POST)

and

            if($count == 1) 
            {
                $_SESSION['authenticated'] = true;
                $_SESSION['username'] = $username;
                header('Location:index.php');
            }
        }
            else {
                session_destroy();
                echo ("Error: that username and password combination does not match any currently within our database");
            }

to

            if($count == 1) 
            {
                $_SESSION['authenticated'] = true;
                $_SESSION['username'] = $username;
                header('Location:index.php');
            } 
            else {
                session_destroy();
                echo ("Error: that username and password combination does not match any currently within our database");
            }
        }

Because if you will not send any data, the sessions will be destroyed. 因为如果您不发送任何数据,则会话将被破坏。 I believe you want if sent data are incorrect, then delete any sessions. 我相信如果发送的数据不正确,请删除所有会话。

Well for one if($count == 1) , you don't have an else or else if that handles a failure to find the user in the database. 好吧,对于一个if($count == 1) ,您就没有else了,否则就解决了在数据库中找不到用户的问题。

I'd move the else clause you have to after that if and put an else after the parent if that provides a message about missing input parameters instead of nothing matching. 我想移动else你必须经过该条款if ,把一个else父后if提供有关失踪的输入参数,而不是什么都不匹配的消息。

Here is the complete code for contact us form working perfectly, you just need to edit this form according to your need and add to your html code as post form. 这是与我们完美联系的表格的完整代码,您只需要根据需要编辑此表格并将其添加为html代码即可。 i am using this same code on my website. 我在我的网站上使用了相同的代码。

 <?php

 if(isset($_POST['email'])) {



// EDIT THE 2 LINES BELOW AS REQUIRED

$email_to = "your@gmail.com";

$email_subject = "Your email subject line";





function died($error) {

    // your error code can go here

    echo "We are very sorry, but there were error(s) found with the form you submitted. ";

    echo "These errors appear below.<br /><br />";

    echo $error."<br /><br />";

    echo "Please go back and fix these errors.<br /><br />";

    die();

   }



// validation expected data exists

if(!isset($_POST['first_name']) ||

    !isset($_POST['last_name']) ||

    !isset($_POST['email']) ||

    !isset($_POST['phone']) ||

    !isset($_POST['comments'])) {

    died('We are sorry, but there appears to be a problem with the form you submitted.');       

   }



$first_name = $_POST['first_name']; // required

$last_name = $_POST['last_name']; // required

$email_from = $_POST['email']; // required

$telephone = $_POST['phone']; // not required

$comments = $_POST['comments']; // required



$error_message = "";

$email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';

 if(!preg_match($email_exp,$email_from)) {

  $error_message .= 'The Email Address you entered does not appear to be valid.<br />';

   }

$string_exp = "/^[A-Za-z .'-]+$/";

  if(!preg_match($string_exp,$first_name)) {

  $error_message .= 'The First Name you entered does not appear to be valid.<br />';

}

 if(!preg_match($string_exp,$last_name)) {

$error_message .= 'The Last Name you entered does not appear to be valid.<br />';

  }

 if(strlen($comments) < 2) {

   $error_message .= 'The Comments you entered do not appear to be valid.<br />';

    }

 if(strlen($error_message) > 0) {

  died($error_message);

   }

$email_message = "Form details below.\n\n";



function clean_string($string) {

  $bad = array("content-type","bcc:","to:","cc:","href");

  return str_replace($bad,"",$string);

    }



$email_message .= "First Name: ".clean_string($first_name)."\n";

$email_message .= "Last Name: ".clean_string($last_name)."\n";

$email_message .= "Email: ".clean_string($email_from)."\n";

$email_message .= "Telephone: ".clean_string($telephone)."\n";

$email_message .= "Comments: ".clean_string($comments)."\n";





// create email headers

 $headers = 'From: '.$email_from."\r\n".

 'Reply-To: '.$email_from."\r\n" .

 'X-Mailer: PHP/' . phpversion();

 @mail($email_to, $email_subject, $email_message, $headers);  

 ?>



  <!-- include your own success html here -->



  <h4 align="center">Thank you for contacting us. We will revert you back.</h4>



 <?php

   }

?>

First of all don't use MySQL, please use MySQLi, Second, try to not use the sprintf function. 首先不要使用MySQL,请使用MySQLi,其次,请不要使用sprintf函数。 Try using this code 尝试使用此代码

mysql_query('select * from users where username = UPPER($username) and password = UPPER($password)"

Sorry im using a phone to answer this 抱歉,我正在用手机接听电话

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM