我现在使用PHP一年了。我曾经使用一种方式来构建我的网站,我认为它不适合具有多个接口的应用程序(web,Android,IOS ..)。我只是创建一个html页面或一个表单,然后在PHP脚本中处理输入。这是一个登录脚本的简单示例:
这是登录表单:
<?php
$error = '';
if(isset($_GET['error']))
$error = $_GET['error'];
?>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<link href="dist/css/bootstrap.css" rel="stylesheet">
<title>Blogmaker Home</title>
</head>
<body>
<div class="container">
<nav class="navbar navbar-default" role="navigation">
<!-- Brand and toggle get grouped for better mobile display -->
<div class="navbar-header">
<a class="navbar-brand" href="#">BlogMaker</a>
</div>
<br><br>
</nav>
<!-- Static navbar -->
<!-- Main component for a primary marketing message or call to action -->
<div class="row">
<div class="col-md-8">
<div class="jumbotron">
<h1>BlogMaker</h1>
<p>You want to create your blog for free! You can do it right now.
Just click in the "Create My Blog" button below and start posting and receiving comments
from other peoples.
</p>
<p>
<a class="btn btn-lg btn-primary" href="Signup.php" role="button">Create My Blog</a>
</p>
</div>
</div>
<div class="col-md-4">
<?php
if($error=='')
echo'<br><br>';
else
echo'<div class="alert alert-danger">'.$error.'</div>';
?>
<form class="form-signin" action="login.php">
<h2 class="form-signin-heading">Please sign in</h2>
<input type="text" class="form-control" placeholder="Email address" required autofocus name="username">
<input type="password" class="form-control" placeholder="Password" required name="pw">
<button class="btn btn-lg btn-primary btn-block" type="submit">Sign in</button>
</form>
</div>
</div>
<div class="footer">
<p>© BlogMaker 2013</p>
</div>
</div> <!-- /container -->
<!-- Bootstrap core JavaScript
================================================== -->
<!-- Placed at the end of the document so the pages load faster -->
<script src="https://code.jquery.com/jquery-1.10.2.min.js"></script>
<script src="dist/js/bootstrap.min.js"></script>
</body>
</html>
以下是PHP脚本中处理的登录过程:
<?php
session_start();
require_once 'DATABASE-CONFIG.php';
$username='';
$pw='';
if(isset($_GET['username'])&&isset($_GET['pw'])){
$username=$_GET['username'];
$pw=$_GET['pw'];
mysql_connect(SERVERNAME,DBUSERNAME,DBPASSWD) or die('Fail to connect to DB server');
mysql_select_db(DBNAME)or die('Fail to find the DB in the server');
$query = "SELECT * FROM users WHERE username='$username'";
$result = mysql_query($query);
mysql_close();
if(mysql_num_rows($result)>0){
$_SESSION['username']=$username;
header('Location:myblog.php');
}
else
header('Location:index.php?error=Please check your username and your password!');
}else
header('Location:index.php?error=Username and password are mandatory!');
?>
如您所见,我按名称调用html输入字段并检查用户输入。我认为建立一个网站是一种不好的做法。所以我的问题是在PHP中创建一个处理多个接口的Web应用程序的最佳实践是什么?我只想要主要概念。一个小例子也是非常好的。
由于
答案 0 :(得分:1)
我会考虑构建一个RESTful API。通过这种方式,您可以编写一次所有业务逻辑,并让多个客户与其进行交互(网站,移动应用等。这是一个开始的好地方:API tutorial
答案 1 :(得分:1)