Skip to main content

PHP Tutorial - Lesson 3: Introduction to PHP - Part III


PHP and HTML
          At beginning of our php tutorial, we said that php can embed any language inside it such as html, and it might work like (CGI).
          Now, we are going to work with graphical user interface (GUI).
          Hope you’ll enjoy your time while working with something great, useful, and simple.

      One of the most important things, that makes user like your site, and come back again, the design of your site. It is not a secret, when I tell you that all php web applications depends completely on html and some JavaScript, that makes your site active, and finally, some CSS styles.
<?php
echo ‘<font style=“font-size:15px; font-family:arial; color:#000066; font-weight:bold;”>Welcome to my site</font>’;
?>
          When writing php code that embeds other scripting language, hope you follow these rules.
          When you embed non-php script with php, we deal with non-php script as a string.
          So we might store the code with a variable, and use it as needed, or echo it directly.
          PHP strings are enclosed within single or double quotes. If your string enclosed within single quotes, then you can use double quotes inside your string, and vice versa. Single quotes are container for double quotes. And so double quotes are container for single quotes.
Forms
<?php
// Login form
// Programmer Anas Jaghoub
// Created on 12/oct/2009
// loginForm.php
$loginForm = ‘<form name=“login” method=“post” action=“authenticate.php”>
Username: <input type=“text” name=“username”><br>
Password: <input type=“password” name=“password”> <br>
<input type=“submit” value=“Sign in”>  <input type=“reset” value=“Reset”>
</form>’;
echo $loginForm;
?>

<?php
// authenticate username and password
// programmer Anas Jaghoub
// created on 12/oct/2009
// authenticate.php
$username = $_POST[‘username’];
$password = $_POST[‘password’];
if( ($username == “anas”) && ($password==“123456”) )
{
// authentication true
// he is a user not a guest
echo “Logged in successfully<Br>”;
$expiration = time() + 60*15 ; // make user logged for only 15 minutes.
setcookie(“user”,”$username”,$expiration);
}
else
{
// authentication false
echo “authentication failed. Please check that you entered the correct username and password”;
echo “<a href=loginForm.php>Try again</a>”;
}
?>
Notes on loginForm.php script:
          We made a form using html form tag. Then stored it in a variable called $loginForm.
          Please note that you can design your form as you like, but follow the same rule of single and double quotes.
          Our form method is post. What does it mean? It means how to send our form data. In fact you can use two methods which are post and get. Each method has properties and advantages.
          Post method: sends your form data to the destination, without propagating it with the URL, this way give it an advantage of security. So your credentials will not be seen in the history of visited sites. Another advantage it is unlimited. So by using post method, you can send as big as needed data, without facing limited size issues.
          On the other hand, GET method, sends your form data propagated with the URL for the destination,  and has limit size.
          We might use GET method when getting information from user, and there is no scare of showing the data to any user. For example getting an article from database.
          When using get method then data will be sent to the destination like this: yourdomain.com/loginForm.php?username=“value of username field” & password=“value of password field”. So it should not be used with sensitive information. Nevertheless, it has an advantage of ability to bookmark.
          When using post method data is not propagated with URL, yourdomain.com/loginForm.php
Notes on authenticate.php script:
          This script supposes that username correct value is “anas” and password correct value is “123456”.
          PHP offers three built-in arrays that deals with form data. $_POST[], $_GET[], and $_REQUEST[].
          $_POST is used when form method is post.
          $_GET is used when form method is get.
          Finally, $_REQUEST is used in both post and get methods.
          Enhancement for loginForm.php script:
          As you noticed from the script. It will show login form for all users and guests. When a user enters his username and password correctly, he doesn’t have to enter it again, as long as the cookie file created in the authenticate.php has not expired. So we want to check if the visitor of the page is a user or a guest, by checking for the cookie file is set or not.
<?php
// Login form
// Programmer Anas Jaghoub
// Created on 12/oct/2009
// last modified in 12/oct/2009
// loginForm.php
$loginForm = ‘<form name=“login” method=“post” action=“authenticate.php”>
Username: <input type=“text” name=“username”><br>
Password: <input type=“password” name=“password”> <br>
<input type=“submit” value=“Sign in”>  <input type=“reset” value=“Reset”>
</form>’;
If(isset($_COOKIE[‘user’])) echo “Welcome “. $_COOKIE[‘user’].”<Br>”;
else echo $loginForm;
?>
Notes:
          All what we did in our script, is checking for a cookie file called user, if this cookie file is set. Then he is a user and he might has more privileges, Remember that we set the cookie user after we checked that he has correct username and password.
          Remember, function isset returns true or false depending on the parameter. In our case we’ll check that $_COOKIE[‘user’] has a value or is null, if it has a value, then expiration date has not been reached. else then maybe expiration time reached and cookie has been deleted, or given data from guest is not correct.

End of Lesson 3, To download this lesson as PDF file click here: PHP Tutorial - Lesson 3: Introduction to PHP - Part III

Best Wishes,
Anas Jaghoub

Comments

Popular posts from this blog

PHP Tutorial–Guest Book–System Definition and Requirements

In this lesson I’m going to illustrate the system definition and requirements for the Guest Book application. It is an important step in developing any application, since this step gives us an orientation about the system and its functionality, what is expected from the system to do. actually the system definition comes from keywords from customers and end users, usually try to make it clear for what they need in the application, and what they expect the system to do.  As developers it is our role to define the requirements for the system to be developed. In our example the system definition for the Guest Book is: A system that allows visitors of the site to post their comments and feedbacks about the site, with the possibility for managing comments and maintain it easily and user friendly. On the other hand, the system requirements are: a web server, since it is clear that the developed system is going to run on the Internet, so it is a web-based application not windows-based.

تعلم تطوير تطبيقات للموبايل باستخدام الأندرويد

مرحبا أصدقائي يسعدني أن أبدأ معكم سلسلة حلقات في تعلم تطوير تطبيقات للموبايل باستخدام الأندرويد ، وسأسعى جاهدا معكم في أن تكون هذه السلسلة من أوائل السلسلات في اللغة العربية لتعليم برمجة تطبيقات الأندرويد من البداية وحتى الاحتراف. وسأحاول قدر المستطاع الشرح بلغة عربية بسيطة ومفهومة. حيث ستكون غدا إن شاء الله الحلقة الأولى من تعلم تطوير تطبيقات الموبايل باستخدام الأندرويد. أنس الجاغوب Twitter: @anasjaghoub Facebook: anasjaghoub

Web Development Best Practices (PHP Best Practices)

Websites development is one of the most challenging projects ever to develop, according to the large number of components you deal with, integrate, and manage in your application. For example, most web projects Deal with databases as their data source, and I ntegrate with external services called web services and APIs. As well the application M anages its own objects and components from views to models to controllers to plugins and so on. A skilled web developer should have a great/deep knowledge in most of components used in his system, and as well should ha ve intensive focus on how each component work and how can be used, what can be done, and what cannot. On the other hand, the huge amount of requirements, components to manage, and time frame given to you to accomplish your mission, and number of people in your team, all these factors together requires from you to organize your tasks, and priorities your requirements, do deep analysis and planning in order to have a hi