dpd7195 2015-02-25 16:33
浏览 26

构建处理多个接口的Web应用程序的正确方法是什么?

I am using PHP for a year now. I used to build my website using a way that i think it will not be suitable for an application that has multiple interfaces (web, Android, IOS..). I simply create an html page or a form and then handle the inputs in PHP script. This is a simple example of a login script:

This is the login form:

<?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>&copy; 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>

Here is the login process handled in a PHP script:

<?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!');

?>

As you see, i call the html input fields by name and check user input. I believe this is a bad practice to build a website. So my question is what is the best practice to create a web application in PHP that deals with several interfaces? i just want the main concept. Also it would be very good with a small example.

Thanks

  • 写回答

2条回答 默认 最新

  • dongliangkeng1056 2015-02-25 17:13
    关注

    I would look into building a RESTful API. This way you can write all of your business logic once and make several clients interact with it (website, mobile apps, etc. Here is a good place to get started: API tutorial

    评论

报告相同问题?

悬赏问题

  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?