如何从外部php文件访问网页

时间:2018-06-14 12:07:18

标签: php

我是PHP的新手,我试图创建一个简单的注册页面。我为HTML和PHP制作了两个单独的文件。所以在post方法中我包含第二个文件:<form action="register.php" method="post">。问题是在提交注册后我想检查所有这些列,如果有错误,请提供一些相关信息。但一切都在一个新的空白页面打开。我想在单独的文件中维护PHP和HTML。的index.php:

<!DOCTYPE html>

<html lang="pl">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<link rel = "stylesheet" type = "text/css" href = "main.css">
<link href="https://fonts.googleapis.com/css?family=Lato:400,700"       
rel="stylesheet">
<link rel="stylesheet"   
href="https://use.fontawesome.com/releases/v5.0.13/css/all.css" 
integrity="sha384- 
DNOHZ68U8hZfKXOrtjWvjxusGo9WQnrNx2sqG0tfsghAvtVlRW3tvkXWZh58N9jp" 
crossorigin="anonymous">
<title> Rejestracja</title>
</head>

<body>
<div id = "imover">
    <nav><div id = "navcontainer"><ul>
        <li><a href = "#">Home</a></li>
        <li><a href = "#">About us</a></li>
        <li><a href = "#">Our projects</a></li>
        <li><a href = "#">Contact</a></li>
        </ul></div></nav>
    <div id = "socialscontainer"><div id = "fb"><a href = "#"><i class="fab 
fa-facebook-square"></i></a></div><div id = "tw"><a href = "#"><i class="fab 
fa-twitter-square"></i></a>
</div><div style = "clear: both;"></div>

    </div>
    <header><div id = "logocontainer"><h1>Register</h1></div></header>
    <div id = "container">
        <form action="register.php"  method="post">
            <h2>Username</h2><input type = "text" name = "username">
            <h2>Password</h2><input type = "password" name = "pass1">
            <h2>Confirm password</h2><input type = "password" name = "pass2">
            <h2>E-Mail</h2><input type = "text" name = "email">
            <br/><br/>
            <input type = "submit" id ="btn" value="Register now!">
        </form>
    </div>
            <span><a href = "logowanie.php">Do you have an account? Log in 
 now!</a></span>
    </div>

</body>
</html>

还有register.php:

<?php
session_start();
$username = $_POST['username'];
$password = $_POST['pass1'];
if($_POST['pass2'] !=$password) {header("Location: mistake.php"); exit();}
$hashed_password = password_hash($password,PASSWORD_DEFAULT);
$email = $_POST['email'];

echo $username;
?>

1 个答案:

答案 0 :(得分:1)

如果你想留在同一个网站上,你需要删除表格标签中的'action'属性并使用你的php文件,添加
<?php include ('register.php') ?>
在你的HTML顶部。

也许你需要将'.html'更改为'.php',但这不是问题,我希望。

另外你可能需要像这样添加......

  if ( isset($_POST['username'])){      
        $username = $_POST['username'];
        $password = $_POST['pass1'];
        if($_POST['pass2'] !=$password) {header("Location: mistake.php"); exit();}
        $hashed_password = password_hash($password,PASSWORD_DEFAULT);
        $email = $_POST['email'];

        echo $username;
    }

否则它可能会崩溃,因为$ _POST中没有值,那么你第一次进入网站