mercredi 25 février 2015

How to build a web application that is flexible with several interfaces using PHP

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="http://ift.tt/1baGyH0"></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





Aucun commentaire:

Enregistrer un commentaire