Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 CS428 Web Engineering Lecture 22 Building Dynamic Web pages (PHP - V)

Similar presentations


Presentation on theme: "1 CS428 Web Engineering Lecture 22 Building Dynamic Web pages (PHP - V)"— Presentation transcript:

1 1 CS428 Web Engineering Lecture 22 Building Dynamic Web pages (PHP - V)

2 There are three ways that we get data from our users on the web. User can provide information to server through three ways. URL’s/LinksGET FormsPOST CookiesCOOKIE INTERACTING WITH USER

3 EXAMPLE One or Multiple values from URL, & is use to separate values. Firstpage.php Second Page Secondpage.php <?php print_r($_GET); $id = $_GET[‘id’]; $name = $_GET[‘name’]; echo “ ”. $id. “: {$name} ”; ?> If we want multiple key pair values, then we separate one value with other through &.

4 urlencode() What if the & is included in the name. we could come with some kind of string that we passes &, % # sign. Firstpage.php &id=42”>Second Page Secondpage.php <?php print_r($_GET); $id = $_GET[‘id’]; $name = $_GET[‘name’]; echo “ ”. $id. “: {name} ”; ?>

5 htmlspecialchars() Sometimes we generate HTML, we want to have HTML tag in a link. We can handle it through PHP. Firstpage.php <?php $linktext = “ second page”; ?> &id=42”> <?php echo htmlspecialchars($linktext); ?> When your web server read this code, then due to htmlspecialchars command, if in string we have HTML characters, it ignores it in-fact treat it simple plain text.

6 FORMS Forms.php Username: Password:

7 Process.php (we just want to display values back to our page) <?php $username = $_POST[‘username’]; $password = $_POST[‘password’]; echo “{$username}: {$password}”; ?>

8 COOKIE Another way to interact with our users in user’s browser is COOKIES. Cookies are very useful, putting data on user’s browser. Our web server make a request to their web browser to send back a cookie. Cookies are going to be access in the same way as POST. In cookies you will learn an additional thing the SET cookie.

9 COOKIE Whenever we read a cookie, its going to be a very good practice. "; echo "Value is: ". $_COOKIE[$cookie_name]; } ?>

10 SESSION Session is a file that store on web server. And in that file we can store whatever information we want. The way we find out which file belongs to which user is by using a cookie. So we set a special session cookie on their browser, and then we look for that cookie to find the place where file resides on our server. And then we look inside that file, to find the information stored in that session.

11 EXAMPLE We will go above the HTML and make sure the session start commands. <?php session_start(); $_SESSION[“username”] = “user”; $_SESSION[“password”] = “123456”; ?> …… <?php session_unset(); // remove all session variables session_destroy(); // destroy the session ?>

12 HEADER - EXAMPLE <?php // this is how you redirect a page header(“Location: basic.html”); exit; ?> …

13 INCLUDE Through include feature you may include another PHP file in our PHP. This is going to help us, do not repeat ourselves. That’s always a good thing. Includes.php <?php include(“header.php”); include(“side-bar.php”); include(“footer.php”); ?>

14 Now there is variation in include, there is also be using REQUIRE. <?php require(“config.php”); ?> Require do basically the same thing as Include does. But it throws an error, if file is not able to loaded for some reasons. Include try to load the file but it wouldn’t generate the error. We can handle with require whenever something like functions that is definitely going to be required. And use include whenever we are loading HTML or something that’s not absolutely essential. There is one last variation in include and require, which is require_once. <?php require_once(“included_func.php”); ?> REQUIRE


Download ppt "1 CS428 Web Engineering Lecture 22 Building Dynamic Web pages (PHP - V)"

Similar presentations


Ads by Google