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 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)