我创建了一个登录应用程序。当我输入用户名和密码时,它总是在页面中显示PHP代码。它不运行代码,只是显示它为什么?
第二件事是,我使用EasyHP创建了数据库,但我不知道我的网站是否可以访问该数据库。我把网站放在EasyHP本地web文件中,这是我的代码:
html文件:
<!DOCTYPE html>
<html>
<head>
<title>Library</title>
<meta charset="UTF-8">
<style type="text/css">
html {
font-family: Verdana, Geneva, sans-serif ;
}
h1 {
font-size: 24px ;
text-align: center ;
}
#wrapper {
top: 30%
margin: 0 auto
}
#form {
margin: auto ;
width: 200px ;
heigh: 100px ;
}
</style>
</head>
<body>
<div id = "wrapper">
<h1>Library log in</h1>
<form id = "form" action="login.php" method="post" >
UserName: <input type="text" name="username"> <br>
Password: <input type="password" name="password"> <br>
<input type="submit" value="Login" name="submit" >
</form>
</div>
</body>
</html>
php文件:
<?php
session_start();
$username = $_POST['username'];
$password = $_POST['password'];
if ($username&&$password){
$connect = mysql_connect("localhost","root",""); or die ("Couldn't connect") ;
mysql_select_db("phplogin") or die ("Couldn't find db") ;
}
else {
echo "invalid username or password";
}
?>
数据库名称为phplogin
我从硬盘打开文件,而不是从网络主机打开。
错误是当我运行网站,然后填写表单并按下登录按钮时,它会显示PHP代码。它应该将我发送到一个空页面,或者在数据错误的情况下显示一条错误消息。
我已经更改为session_start();
相同的错误。
首先确保扩展名是.php
而不是.html
接下来你的session_start
错了,把它变成session_start()
然后确保您在本地主机上运行,您可以尝试xammp
试试这个。
<?php
session_start();
$connect = mysql_connect("localhost","root",""); or die ("Couldn't connect") ;
mysql_select_db("phplogin") or die ("Couldn't find db") ;
$username = $_POST['username'];
$password = $_POST['password'];
if ($username&&$password){
//create a file success.php
header("location:success.php");
}
else {
echo "invalid username or password";
}
?>
http://coolestguidesontheplanet.com/get-apache-mysql-php-phpmyadmin-working-osx-10-10-yosemite/
请参阅此链接以配置apache和php for mac