dongshu9458 2017-10-27 06:50
浏览 96
已采纳

创建一个简单的密码登录,没有密码硬编码[PHP,Javascript,MySQL]

I'm trying to create a simple login promt on my local website. I already tried with Javascript, but I don't want the password to be hardcoded. The Users get the password by mail so there is no registration form needed. I searched on the Internet and I think it should work with PHP and Javascript. This is what i've come up with:

<SCRIPT>
function passWord() {
var testV = 1;
var pass1 = prompt('Enter password',' ');
while (testV < 3) {
if (!pass1)
window.open('Website.html',"_self");
if (pass1.toLowerCase() == "password") {
alert('Correct!');
window.open('./test/sitetwo.html',"_self");
break;
} 
testV+=1;
var pass1 = 
prompt('Wrong Password','Try again');
}
if (pass1.toLowerCase()!="password" & testV ==3) 
return " ";
} 
</SCRIPT>
<CENTER>
<FORM>
<input type="button" value="Enter Protected Area" onClick="passWord()">
</FORM>
</CENTER>

Does anyone of you know how to code this? Thank you for your help.

  • 写回答

2条回答 默认 最新

  • douba9425 2017-10-27 07:11
    关注

    In my opinion, you should use a database to store all your credentials (like username, password, etc..)

    If you don't know how to do it, you should know that if you want to run your php code, you need a php server and somewhere to put your db.

    Here is how to set up a php server with Apache

    https://www.ultraedit.com/support/tutorials-power-tips/uestudio/local-php-mysql-dev-environment.html

    Here is how to set up a db with PhpMyAdmin https://www.siteground.com/tutorials/phpmyadmin/create-populate-tables/

    You need a login.php (where you log in), a test.php page (then you put in it whatever you want) and a check_User.php page (where to control if the credentials are correct).

    Login.php

    <html>
    <head> <title>Login</title> </head>
    <body>
    <form action="check_User.php" method="post" id="login_form">
    <label><b>Username</b></label>
    
                    <!-- USERNAME -->               
                    <input type="text" placeholder="Enter Username" name="username" required>
    
                    <!-- PASSWORD -->                   
                    <label><b>Password</b></label>
                    <input type="password" placeholder="Enter Password" name="password" required>
    
                    <!-- LOGIN -->  
                    <button type="submit">Login</button>
    </form>
    <body>
    </html>
    

    check_User.php

    <?php
       session_start();
       $_POST["username"] = htmlspecialchars($_POST["username"]);
       $_POST["password"] = htmlspecialchars($_POST["password"]);
       $link = mysqli_connect("your_host", "your_db_username", "your_db_password", "your_db_name");
    
       $query = "SELECT username, password 
                FROM your_db_name
                WHERE username = \"".$_POST["username"]."\" AND password = \"".$_POST["password"]."\"
               ";
       mysqli_query($link, $query);
    
       $rows = array();
       $result = mysqli_query($link, $query);
       while($row = mysqli_fetch_array($result,MYSQLI_ASSOC)) 
            $rows[] = $row;
    
       /*   correct match  */
       if(mysqli_affected_rows($link) == 1)
       {
          $_SESSION["username"] = $_POST["username"];
          $_SESSION["password"] = $_POST["password"];
       }
       if(isset($_SESSION["username"]) && isset( $_SESSION["password"]))
            header("Location:test.php");
       else {
       alert("Wrong username or password");
       }
       ?>
    

    test.php

       <?php
       session_start();
    
        // not logged in, not showing this page
        if((!isset($_SESSION["username"]) || !isset( $_SESSION["password"]))
            header("Location:login.php");
       ?>
    
       <html>
       ....whatever you want this page to do
       </html>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀