javascript 和 php - 登录脚本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17136725/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 07:13:00  来源:igfitidea点击:

javascript and php - login script

phpjavascriptlogin-script

提问by Dominika

I am begginer in programming, I commented the rows, where I dont understand the code. And the connection to database is ok, so I do not understand why it is not working.

我是编程初学者,我评论了我不理解代码的行。并且与数据库的连接正常,所以我不明白为什么它不起作用。

HERE IS LOGIN.PHP

这是登录.PHP

<?php

include("connectiondbs.php");
include("javascript.js");
session_start();
if($_SERVER["REQUEST_METHOD"] == "POST")
{
$u=$_POST['username'];
$p=$_POST['password'];

$sql="SELECT userID FROM users WHERE login='$u' and password='$p'";
$result=mysql_query($sql);
$row=mysql_fetch_array($result);

$active=$row['active'];            //I AM NOT SURE ABOUT THIS ONE

$count=mysql_num_rows($result);

if($count==1)
{
session_register("username");
session_register("password");     //HAS TO BE THE PASSWORD IN REGISTER?
$_SESSION['login_user']=$u;

header("location:succesfulllogin.php");  // HAS TO BE THIS FILE? OR I CAN GO TO THE MENU.HTML RIGHT NOW

}
else {
echo "Wrong Username or Password";
}}
?>

<form action="" method="post">
<label>Login :</label>
<input type="text" name="username"/><br />
<label>Password :</label>
<input type="password" name="password"/><br/>
<input type="submit" value=" Submit "/><br />
</form>

JAVASCRIPT.JS

JAVASCRIPT.JS

<script Language="JavaScript">
    <!-- 
        function check_empty_field()
        {
        if (form.username.value == "" || form.password.value == "")
        {
           alert("Please fill in the password or login field.");
           return (false);
        }
        return (true);
        }
        -->
</script>

succesfulllogin.php

成功登录.php

<?php
session_start();
if(!session_is_registered(username)){
header("location:login.php");
}
else{
header ("location:menu.html");
}
?>

回答by Kylie

You don't have to go to successfullogin.php......as long as you put this at the top of your menu.html file.....which means you would rename it to menu.phpfile. This will just deter people from going straight to menu.php....without being logged in.

你不必去successfullogin.php......只要你把它放在你的 menu.html 文件的顶部......这意味着你可以将它重命名为menu.phpfile.html 。这只会阻止人们在未登录的情况下直接访问 menu.php ....

<?php
session_start();
if(!session_is_registered(username)){
header("location:login.php");};?>

And...no you dont have to store the password in session, unless its being read somewhere else later down the line in your site. Check to make sure, but I doubt it is. You probably don't need to store it. So you can probably remove this line....

而且...不,您不必将密码存储在会话中,除非稍后在您的站点中的其他地方读取密码。检查以确保,但我怀疑它是。您可能不需要存储它。所以你可能可以删除这一行....

    session_register("password");     //HAS TO BE THE PASSWORD IN REGISTER?

You are also correct in being skeptical about this line......doesnt seem to be doing anything....you can probably remove it as well...

你对这条线持怀疑态度也是正确的......似乎没有做任何事情......你也可以删除它......

   $active=$row['active'];            //I AM NOT SURE ABOUT THIS ONE

回答by TheEnvironmentalist

To start, your javascript isn't being run. You're creating the function, but not calling it, and it doesn't interact with the PHP in any way. You can do that by changing your form tag to this:

首先,您的 javascript 没有运行。您正在创建函数,但没有调用它,并且它不会以任何方式与 PHP 交互。您可以通过将表单标签更改为:

<form action="login.php" method="post" onsubmit="return check_empty_field()">

Now the function check_empty_field runs whenever you submit the form. If it returns true, the form gets submitted and the PHP runs. Otherwise, it doesn't. For the function, change it to this (I put in some jQuery because it makes it 10x easier, if you're new to programming you should definitely look into it):

现在,无论何时提交表单,函数 check_empty_field 都会运行。如果返回 true,则提交表单并运行 PHP。否则,它不会。对于函数,将其更改为此(我放入了一些 jQuery,因为它使它变得简单 10 倍,如果您不熟悉编程,则绝对应该研究它):

<script Language="JavaScript">
  <!--
    function check_empty_field()         // This is called when the submit
    {                                    // button is pressed
      if (form.username.value == "" || form.password.value == "")
      {
        alert("Please fill in the password or login field.");
        return false;                    // This doesn't submit the form
      }
      else
      {
        return true;                     // This submits the form
      }
    }
  -->
</script>

The jQuery makes a lot of this much easier, which is why I added it in. You can find more about it here. Of course, you'd have to put this at the top of your page, to make the jQuery work:

jQuery 让这一切变得更容易,这就是我添加它的原因。您可以在此处找到更多相关信息。当然,你必须把它放在页面的顶部,让 jQuery 工作:

<script language="javascript" src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-2.0.1.min.js"></script>

This gets the jQuery definitions from the Microsoft CDN. It's rather reliable. It would be better to just put jQuery on your site, though, and then put in the path to the file.

这从 Microsoft CDN 获取 jQuery 定义。还是比较靠谱的。不过,最好将 jQuery 放在您的网站上,然后放入文件的路径中。

Next, the PHP has some issues. Some have been covered by others, and pay attention to the previous answer, but to point out a few issues, session_registeris not used anymore. Now you set session variables with $_SESSION['variablename'] = 'value'. You also have other issues to worry about, like validation and such. You should Google that.

接下来,PHP 有一些问题。有的已经被别人覆盖了,注意前面的回答,但指出几个问题,session_register不再使用了。现在您使用$_SESSION['variablename'] = 'value'. 您还有其他问题需要担心,例如验证等。你应该谷歌一下。

If you want to know more about any of this, feel free to comment. I also can provide you with sources so you can learn more about the different parts of the code I used.

如果您想了解更多关于这方面的信息,请随时发表评论。我还可以为您提供资源,以便您可以了解有关我使用的代码的不同部分的更多信息。