Skip to main content

PHP Tutorial - Lesson 2: Introduction to PHP - Part I

Control Structures
Cookies and Sessions
•Before starting control structures in php, am going to illustrate two important concepts in web applications area, Cookies and Sessions.
•What is a Cookie?
•a cookie is a file embedded from the server to the user's computer. Its purpose is identifying a user. 
•When you deal with web applications, actually you don’t know who the user that uses your application is. Unlike traditional or real-life applications, For example, if you work in a bank, and a user asks you to exchange him money from his bank account, you have some remarks that identify this person, such as: his name, identification number, his picture, his signature, and more details that distinguish him from others.
•If we want to apply this job online, we don’t know the user who deals with the application. So we make some procedures that help us distinguish one user from another. First thing is asking the user for a username, and password. Second thing is checking for compatibility with registered data in our database. Thirdly, if it is compatible then we’ll create a cookie that distinguishes the user from a guest. In some cases the user has privileges more than a guest. User could access data, where guest is not allowed.
•Otherwise the cookie will not be created, for one of two reasons, first one is the cookie file has already been set in the past, and its expiration date is not reached, or given username and password from the user are not compatible. 
•How to create a cookie?
•To create a cookie in php we use a built-in function called setcookie that has three parameters. First parameter is cookie name, second one is cookie value, and last one is expiration time setcookie(“name”,value,expiration).
Example:
<?php
$expiration = time() + 60 *60 * 1;
// this cookie will expire after an hour.
$value = “anas”;
$name= “user”;
setcookie(“$name”, “$value”, $expiration);
/* this will set a cookie called user,
its value anas, and will expire after an hour from executing this script */
?>
Notes:
•function time()  returns value of current time as a Unix timestamp.
•$expiration value is the time when the cookie will be expired, in our example we need the cookie to expire after an hour from the execution, time() + 60*60*1
•we use this format because function time deals with seconds, which means 60 seconds multiplied by 60 minutes gives an hour, we multiplied it with 1 to determine one hour. we could multiply it with any number of hours.
•$value is the value of the cookie we want to set.
•$name is the name of the cookie we want to set.
•How to retrieve a cookie?
•We might retrieve a cookie using an array called $_COOKIE, we’ll discuss arrays in more details in chapter 4.
•But, now we’ll use it by only telling you that this is an array, and how to use it in your script.
•To retrieve a cookie that has already been set do as followed:
<?php
echo $_COOKIE[“user”]; //  will print cookie user value
?>
Notes:
•We use $_COOKIE[“user”] to get value of a cookie called user,  in our example we used  $_COOKIE  to print its value in the web page using echo.
•How to delete a cookie?
•In some cases we might want to delete a cookie before the expiration time has been reached. For example the user clicked on log out link in your page. When a user asks you to log him out from the page, it means that the user wants to delete information he entered, in another words he wants to delete the cookie file, but in fact, user doesn’t know what is a cookie, or in the best case he has minimum knowledge about cookies. So it is your responsible as a programmer to delete these cookies you had set before, and prevent your user information from being accessed by unauthorized users.
•To delete a cookie we use the same method to set a cookie, which means that we’ll use setcookie function again, but with expiration date in the past, for example $expiration =  time() - 60*60; which means any time in the past.
•setcookie(“$name”, ”$value”, $expiration);
What is a session?
•Php session is a variable used to store information about single user.
•Sessions are available to all pages in one application.
•Sessions are stored on the server for later use, unlike cookies that are stored on user computer.
•Session information is temporarily and will be deleted after the user has left the website.
How do sessions work?
•Sessions work by creating a unique id (UID) for each visitor, and store variables based on this UID. The UID is either stored in a cookie or is propagated with the URL.
•How to start a session?
•To start a session simply call function session_start();  which will start your session.
•This function call must be before the <html> tag.
How to store a session variable?
•As we said before sessions have many advantages such as, it is being stored on the server, deleted after user navigates from your site, has UID, and more.
•One of the applications that you might use session in it, is getting online users and guests.
•To store a session we use built-in array called $_SESSION[];
•Below an example for starting a session called views, which tells the visitor of your application, how many pages he had visited, we’ll now only put the structures for this application, and it will be improved and developed after taking a deep look at control structures.
<?php
// Page views
// programmer Anas Jaghoub
// created at 11/oct/2009
session_start();
$_SESSION[‘views’] = 1;
// this starts a session called views
// and stored temporarily at server.
?>
<html>
<body>
<h1>You have visited <?php echo $_SESSION[‘views’]; ?> pages at this visit to my site</h1>
</body>
</html>
How to destroy a session?
In order to destroy a session in php you could use one of these two built-in functions, unset(session name) or session_destroy();
•Each function has advantages and times to use, depending on its properties, for example unset($_SESSION[‘views’]); this function call will reset $_SESSION[‘views’] and return it to the beginning status which is 1.
•Where calling session_destroy(); will completely delete your session and you will loose all your stored data.

PS, To download this lesson as PDF file click here Lesson 2: Introduction to PHP - Part I
Best Wishes, 
Anas Jaghoub 

Comments

Popular posts from this blog

سلسلة تعلم برمجة تطبيقات الاندرويد - إعداد بيئة العمل - الحلقة رقم 1

في هذه الحلقة سنقوم بتجهيز بيئة العمل، وإعداد كافة الأدوات والبيئة اللازمة للبدء في برمجة تطبيقات الاندرويد، لنتمكن في الحلقات القادمة من العمل والتطوير باستخدام الأدوات التي سنقوم بإعدادها في هذه الحلقة. 1. للبدء في تطوير أي برنامج على الاندرويد لا بد من توفر حزمة التطوير الأساسية وهي Android SDK . هذه الحزمة هي حجر الأساس لعمل أي تطبيق على الاندرويد حيث تحتوي على مجموعة كبيرة من الوظائف الاساسية والبرامج التي تمكننا من التحدث مع نظام التشغيل الاندرويد، وطلب تنفيذ العمليات والحسابات إلى غيره من الوظائف بحسب طبيعة كل تطبيق ووظائفه. وتمكننا أيضا من إنشاء أجهزة افتراضية Virtual Machines تساعدنا على تجربة التطبيقات حتى بدون وجود جهاز اندرويد  2. حزمة الجافا JDK والتي تقوم بترجمة النص البرمجي لحزمة الاندرويد وكذلك النص البرمجي للتطبيق وتحويله إلى نسخة تنفيذية قابلة للعمل على أي جهاز اندرويد 3. بيئة العمل IDE وفي هذه السلسلة سنقوم باستخدام Netbeans وذلك لسهولة استخدامه وسهولة التطبيق عليه. كما يمكن أيضا استخدام بيئة العمل Eclipse . ولغايات تبسيط هذه الحلقة و...

XHTML Tutorial–Lesson 1 Part I

Preamble: This tutorial expects that you’ve the basics of HTML 4.01, and supposes that you are familiar with its syntax and purpose. In this tutorial I’ll try to introduce XHTML concept and why we should use XHTML instead of HTML, we’ll talk about differences between HTML and XHTML. We’ll talk about some of web standards that we have to follow when developing a web-based applications. What is XHTML? XHTML stands for EXtensible Hypertext Markup Language, it is almost identical to HTML 4.01, but it is a cleaner version of HTML. We might look to XHTML from a point of view that it is an HTML defined as an XML application. Above all XHTML is recommended by W3C, and though it’s compatible and supported with all web-browsers. XHTML consists of all HTML 4.01 elements combined together with the structure of XML. Why XHTML? The problem of HTML where on no standardization for web browsers, many web browsers around the world, each browser implemented in a way differs from other browsers,...

سلسلة تعلم برمجة تطبيقات الاندرويد – إنشاء أول تطبيق للاندرويد – الحلقة رقم 2

تحدثنا في الحلقة السابقة عن إعداد بيئة العمل، وأهم الأدوات والحزم اللازمة للبدء في تطوير تطبيقات لنظام التشغيل الاندرويد. سنتحدث في هذه الحلقة عن كيفية إنشاء تطبيق للاندرويد، وما هي مكونات تطبيق الاندرويد والبنية الهيكلية ﻷي تطبيق على الاندرويد. إنشاء تطبيق باستخدام بيئة العمل Netbeans : 1. اضغط على قائمة File 2. اختر New Project. 3. اختر نوع المشروع Android 4. اضغط زر Next. 5. اضبط إعدادات المشروع كالتالي: Project Name: اسم المشروع، يفضل أن يكون اسم المشروع هو نفس اسم التطبيق وذلك لتمييزه عن باقي المشاريع الأخرى Package Name: اسم حزمة المشروع، وهنا يجب اتباع قواعد تسمية الحزم في لغة Java إذ يجب أن تتكون من مقطعين على الأقل ويفصل بين كل مقطع بنقطة، ولا يوجد فراغات. Target Platform: رقم نسخة نظام التشغيل الاندرويد التي يدعمها هذا التطبيق، وهنا لا بد من اختيار أقل رقم يتوافق معه هذا التطبيق لمزيد من التفاصيل شاهد الصورة التالية: 6. اضغط على زر Finish وهكذا نكون قد أنشأنا بمساعدة بيئة العمل Netbeans أول تطبيق للأندرويد حيث تم تصميم هيكل التطبيق، واستدعاء كافة ال...