PHP simple login scrypt (with if function)

PHP简单的登录scrypt(如果功能)

Tags: php login
标签: php login

问题 (Question)

I'm new to PHP and I just want a very simple insecure(for testing) login form.

I wrote this code: but I do not get what I want, can you correct what I did wrong or what should I add, correct ...

 <html>
<body>
<form>
Username: <input type="text" name="name" value="<?php $name ?>" ><br>
Password: <input type"password" name="pass" value="<?php $pass ?>" ><br>
<input type="submit">
</form>
<?php
if ($name == "root" && $pass == "laptop") {

    header("Location: /index.html");
    exit;

}




?>

我是新来的PHP,我只是想要一个简单的不安全(测试)的登录表单。

我写这段代码:但是我没有得到我想要的东西,你能纠正我做错了什么或者我应该添加的,正确的…

 <html>
<body>
<form>
Username: <input type="text" name="name" value="<?php $name ?>" ><br>
Password: <input type"password" name="pass" value="<?php $pass ?>" ><br>
<input type="submit">
</form>
<?php
if ($name == "root" && $pass == "laptop") {

    header("Location: /index.html");
    exit;

}




?>

最佳答案 (Best Answer)

Make two files - one with html and the other one with php function. in html add to form action and method

<html>
<body>
<form action="php_file_name.php" method="post">
Username: <input type="text" name="name" value="" ><br>
Password: <input type"password" name="pass" value="" ><br>
<input type="submit">
</form>

and put in php_file_name.php this php code that you wrote, knowing that you're using POST method, that is:

<?php
    if ($_POST[name] == "root" && $_POST[pass] == "laptop") {
        header("Location: /index.html");
        exit;
    }

It should work then. Of course you can choose other name of php_file_name.php but with changing it accordingly

使两个文件-一个HTML和PHP函数的另一个。在HTML添加到表单和

<html>
<body>
<form action="php_file_name.php" method="post">
Username: <input type="text" name="name" value="" ><br>
Password: <input type"password" name="pass" value="" ><br>
<input type="submit">
</form>

放在php_file_name.php这个PHP写的代码,知道你使用POST方法,即:

<?php
    if ($_POST[name] == "root" && $_POST[pass] == "laptop") {
        header("Location: /index.html");
        exit;
    }

它应该工作。你的PHP写HTML然后发送到浏览器的渲染-一旦完成了发送到退出浏览器。

答案 (Answer) 2

Your PHP writes HTML which is then sent to the browser for rendering - once it has completed sending to the browser it exits. Hence your first mistake appears to be that you haven't understood the seperation of what happens at the server and what happens at the client.

Your next mistake is that your PHP operates on variables which happen to have the same names as form elements - but form vales are not automatically converted into PHP variables.

Your third error is that even when you've fixed the 2 previous problems your script won't work: any HTTP headers must be emitted before writing to the html body - so your redirect isn't going to work. Consider instead:

<?php
if ('root' == $_POST['name'] && 'laptop' == $pass) {
  header("Location: /index.html");
  exit;
}
?>
<html>
 <body>
  <form method='POST'>
   <?php print $_POST['name'] ? "Try again...." : "login..."; ?>
   <br /> 
   Username: <input type="text" name="name" 
        value="<?php print $_POST['name']; ?>" ><br />
   Password: <input type"password" name="pass" 
         value="<?php print $_POST['pass'] ?>" ><br />
   <input type="submit">
  </form>
 </body>
</html>

因此你犯的第一个错误是,你还不明白发生了什么,在服务器和客户端发生的分离。你的下一个错误是,你的PHP操作上有相同的名称作为表单元素-但形成山谷不会自动转化为PHP变量。

你的下一个错误是,你的PHP操作上有相同的名称作为表单元素-但形成山谷不会自动转化为PHP变量。

你的第三个错误是,即使你已经固定的前2的问题你的脚本不工作:任何HTTP头必须发出之前写的HTML的身体,所以你将不会工作。转而考虑:

<?php
if ('root' == $_POST['name'] && 'laptop' == $pass) {
  header("Location: /index.html");
  exit;
}
?>
<html>
 <body>
  <form method='POST'>
   <?php print $_POST['name'] ? "Try again...." : "login..."; ?>
   <br /> 
   Username: <input type="text" name="name" 
        value="<?php print $_POST['name']; ?>" ><br />
   Password: <input type"password" name="pass" 
         value="<?php print $_POST['pass'] ?>" ><br />
   <input type="submit">
  </form>
 </body>
</html>

答案 (Answer) 3

The php code you inserted into the form has no function, it does nothing. I edited your form, so that it sends data to the same page. You can then do:

$name = $_POST['name']; $pass = $_POST['pass'];

Your old code comes after that.

你的PHP代码插入表格没有功能,它没有。我编辑表单,以便将数据发送到同一页。然后你可以做的:

$name = $_POST['name'];$pass = $_POST['pass'];

你的旧代码之后。

答案 (Answer) 4

You can put it in the same file index.php

<?php
if (isset($_POST['submit']))
{
echo 'you submitted name='.$_POST['name'].' And password='.$_POST['pass'].'<br /><br />';
}

echo '<form method="post">
Name <input name="name" type="text" /><br />
Pass <input name="pass" type="password" /><br />
<input name"submit" type="submit" />
</form>';
?>

你可以把它放在同一文件index.php

<?php
if (isset($_POST['submit']))
{
echo 'you submitted name='.$_POST['name'].' And password='.$_POST['pass'].'<br /><br />';
}

echo '<form method="post">
Name <input name="name" type="text" /><br />
Pass <input name="pass" type="password" /><br />
<input name"submit" type="submit" />
</form>';
?>

本文翻译自StackoverFlow,英语好的童鞋可直接参考原文:http://stackoverflow.com/questions/23450514