Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
By K.Gautam<br />
Client Server Architecture<br />
Client Server Architecture<br />
Server Side Scripting<br />Server-side scripting is a <br />	Web server technology<br />Generates Dynamic Web Pages<br />P...
Server Vs Client side Scripting<br />
WEB Application<br />
WEB Application<br />An Application accessed over a network.<br />Runs inside a Web Browser<br />
What is PHP?<br />PHP is a web development language <br />PHP: Hypertext Preprocessor<br />PHP6 <br />Created by RasmusLer...
Why PHP?<br />Open Source<br />Free<br />Very Good set of built-in functions<br />Very Good Database support<br />Great su...
Getting Started with PHP<br />
Basic PHP Program<br />Index.php<br /><HTML><br /><HEAD><br /><TITLE>My first PHP program</TITLE><br /></HEAD><br /><BODY>...
Including files<br />Separate Client and Server code <br /><?php<br />include('/filepath/filename')<br />require('/filepat...
PHP SYNTAX<br />PHP Is Forgiving<br />HTML Is Not PHP<br />PHP’s Syntax Is C-Like<br />PHP is whitespace insensitive<br />...
PHP Is Forgiving<br />Focuses on convenience for the programmer<br />Doesn’t focus that much on correctness<br />
HTML Is Not PHP<br />PHP Syntax applies only within <br />PHP should not be confused with HTML<br /><?php<br />?><br />
PHP’s Syntax Is C-Like<br />PHP is whitespace insensitive<br /><?php<br />$four=2+2;// single spaces<br />$four=2+2;// spa...
PHP’s Syntax Is C-Like<br />PHP is sometimes case sensitive<br /><?php<br />$capital=67;<br />print("Variable capital is $...
PHP’s Syntax Is C-Like<br />PHP is sometimes case sensitive<br /><?php<br />	HELLO();<br />	function hello()<br />	{<br />...
PHP’s Syntax Is C-Like<br />Statements are expressions terminated by semicolons<br /><?php<br />$greeting="Welcome to PHP!...
Expressions and types<br />$value=2+2*"nonsense"+TRUE;<br />Output :  3<br />
Braces make blocks<br /><?php<br />if(3==2+1)<br />print("Good Morning have a nice day<BR>");<br />if(3==2+1)<br />{<br />...
Comments<br /><?php<br />/*  This is<br />    a comment in<br />    PHP */<br />#  This is a comment, and<br />Not a Comme...
Variables<br />All variables in PHP are denoted with a leading dollar sign ($).<br />The value of a variable is the value ...
Variables<br />PHP variables are Perl-like<br />All variables in PHP start with a leading $ sign<br />Variable names must ...
Variables<br />Declaring variables (or not)<br />Assigning variables<br />$pi=3+0.14159; // Double<br />$pi="3 + 0.14159";...
Variable scope<br />You can switch modes if you want<br /><HTML><br /><HEAD><br /><?php<br />$username="Jane Q. User";<br ...
Types in PHP: Don’t Worry, Be Happy<br />No variable type declarations<br />$first_number=55.5;<br />$second_number="Not a...
Type Summary<br />Integers are whole numbers, without a decimal point, like 495.<br />Doubles are floating-point numbers, ...
Type Summary<br />Arrays  are named and indexed collections of other values.<br />Objects   are instances of programmer-de...
Outputting<br />Echo and print<br />Basic constructs of the PHP language<br />
Echo<br />echo"This will print in the user’s browser window.";<br />echo("This will print in the user’s browser window.");...
Print<br />The command print is very similar to echo, with two important differences:<br />Unlike  echo, printcan accept o...
Variables and strings<br />$animal="antelope";<br />$animal_heads=1;<br />$animal_legs=4;<br />print("The $animal has $ani...
PHP Control Structures and Functions<br />Topics <br />Boolean Expressions<br />Branching<br />Looping<br />Terminating Ex...
Boolean Expressions<br />Boolean constants<br />if(TRUE)<br />print("This will always print<BR>");<br />else<br />print("T...
Logical operators<br />
Comparison operators<br />
Branching<br />If-else<br />if($first>$second)<br />{<br />$difference=$first-$second;<br />print("The difference is $diff...
Branching<br />Switch<br />$day=5;<br />switch($day)<br />{<br />case1:echo"Sunday";break;<br />case2:echo"Monday";break;<...
Looping<br />While<br />$count=1;<br />while($count<=10)<br />{<br />print("count is $count<BR>");<br />$count=$count+1;<b...
Looping<br />Do-while<br />$count=45;<br />do<br />{<br />print("count is $count<BR>");<br />$count=$count+1;<br />}<br />...
Looping<br />For<br />$limit=5;<br />for($count=0;$count<$limit;$count=$count+1)<br />print"hello <br>";<br />
Using Functions<br />functionfunction-name($argument-1,$argument-2,..)<br />{<br />  statement-1;<br />  statement-2;<br /...
Functions<br /><?php<br />sayhello("Welcome To PHP");<br />functionsayhello($hello_string)<br />{<br />print$hello_string;...
Functions with return-values<br /><?php<br />$sum=add(5,3);<br />echo"5+3=",$sum;<br />function add($a,$b)<br />{<br />ret...
HTTP Requests<br />HTTP Is Stateless<br />GET Arguments<br />POST Arguments<br />
GET Request<br />
GET Request<br /><html><br /><head><br /><title>Http Get Request </title><br /></head><br /><body><br /><formmethod="GET"a...
GET Request<br />
GET Request<br /><?php<br />echo"Hello ",$_GET['Name'];<br />echo'<br>';<br />echo'from ',$_GET['College'];<br />?><br />
POST Request<br />
POST Request<br /><html><br /><head><br /><title>Http Get Request </title><br /></head><br /><body><br /><formmethod="POST...
POST Request<br />
POST Request<br /><?php<br />echo"Hello ",$_POST['Name'];<br />echo'<br>';<br />echo'from ',$_POST['College'];<br />?><br />
mySQL Database Connectivity<br />
<?php<br />$db_username="phpuser";<br />$db_password="phppassword";<br />$db="phpdb";<br />$db_host="localhost";<br />$db_...
PHP File Handling<br />
PHP File Handling<br />FileForm.php<br /><html><br /><body><br /><formmethod="POST"action="file.php"><br />Name:<inputtype...
PHP File Handling<br /><?php<br />$file=fopen("welcome.txt","a+");<br />$name=$_POST['name'];<br />fwrite($file,$name.'<br...
PHP Arrays<br />Creating Arrays<br />$fruit_basket=array('apple','orange','banana','pear');<br />OR<br />$fruit_basket[0]=...
PHP Arrays<br />$fruit_basket=array(0=>'apple',1=>'orange',<br />2=>'banana',3=>'pear');<br />$fruit_basket=array('red'=>'...
PHP Arrays<br />echo$fruit_basket['yellow'];<br />Multidimensional Arrays<br />$cornucopia=array('fruit'=><br />array('red...
PHP Arrays Deleting<br />Deleting from Arrays<br />$my_array[0]='wanted';<br />$my_array[1]='unwanted';<br />$my_array[2]=...
PHP Arrays Iteration<br /><?php<br />$cornucopia=array('fruit'=>array('red'=>'apple',<br />'orange'=>'orange',<br />'yello...
Randomness<br /><html><br /><head><br /><?php<br />$hex=array('a','b','c','d','e','f',1,2,3,4,5,6,7,8,9,0);<br />$color=''...
Session<br />A PHP session variable is used to store information about, or change settings for a user session. <br />Sessi...
Working with Sessions<br />Session.php<br /><?php<br />session_start();<br />if(!isset($_SESSION['username']))<br />{<br /...
Sessions<br />Login.php<br /><?php<br />if(!isset($_POST['uname']))<br />{<br />echo"<form method='POST' action='' ><br />...
Cookies<br />Login.php<br /><?php<br />if(!isset($_POST['uname']))<br />{<br />echo"<form method='POST' action='' ><br />	...
Cookies<br />Cookie.php<br /><?php<br />if(!isset($_COOKIE['username']))<br />{<br />header('location:login.php');<br />}<...
A simple web application<br />Database Schema<br />
Index.php<br /><?php<br />session_start();<br />if(!isset($_SESSION['uid']))<br />	header('location:login.html');<br />els...
Login.html<br /><h1>Login</h1><br /><formmethod="POST"action="login.php"><br />Username:<inputtype="text"name="USERNAME"/>...
Login.php<br /><?php<br />$conn=mysql_connect('localhost','phpuser','phppassword')ordie(mysql_error());<br />mysql_select_...
Register.html<br /><h1>Register</h1><br /><formmethod="POST"action="register.php"><br />Username:<inputtype="text"name="US...
Register.php<br /><?php<br />session_start();<br />session_destroy();<br />$conn=mysql_connect('localhost','phpuser','phpp...
Main.php<br /><?php<br />session_start();<br />if(!isset($_SESSION['uid']))<br />	header('location:login.html');<br />?><b...
Main.php<br /><?php<br />$conn=mysql_connect('localhost','phpuser','phppassword')ordie(mysql_error());<br />mysql_select_d...
Add.php<br /><?php<br />session_start();<br />if(!isset($_SESSION['uid']))<br />	header('location:login.html');<br />$conn...
Logout.php<br /><?php<br />session_start();<br />session_destroy();<br />header('location:index.php');<br />?><br />
bibliography<br />PHP6 and MySQL bible by Steve Suehring, Tim Converse, and Joyce Park<br />W3schools.com<br />Php.net<br />
Presentation<br />This presentation is available online at http://j.mp/ki0GlO<br />
Php workshop
Upcoming SlideShare
Loading in …5
×

Php workshop

13,060 views

Published on

Published in: Education, Technology
  • IT Pathshala, Noida-Sector-63, offers Guaranteed JOB with PHP Training Programs.

    PAY US ONLY IF YOU GET A JOB!

    CALL US - +91-9871350999 or +91-8800394970

    Technology Centre Address: H-110, Sector-63, Noida (UP)

    Program Takeaways:

    - GUARANTEED JOB in your technology domain
    - Live Major Project development
    - Experience Letter on Training Completion
    - Certification on Training Completion
    - Project Source Code available in CD

    Program Highlights:

    - 100% Guaranteed Job – Don’t pay us if you don’t get a JOB!
    - Industry Ready Courses
    - LIVE International Project Experience
    - Exposure to corporate culture at our IT services facility
    - Networking & Interaction with Industry Experts
    - Training Provided by a reputed IT Company
    - Live Project Source Code made available in CD
    - Technology certification and experience letter at program completion
    - Online Study Content

    CALL US - +91-9871350999 or +91-8800394970

    Our website: www.itpathshala.com
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
  • Be the first to like this

Php workshop

  1. 1. By K.Gautam<br />
  2. 2. Client Server Architecture<br />
  3. 3. Client Server Architecture<br />
  4. 4. Server Side Scripting<br />Server-side scripting is a <br /> Web server technology<br />Generates Dynamic Web Pages<br />Practically invisible to end-user<br />
  5. 5. Server Vs Client side Scripting<br />
  6. 6.
  7. 7. WEB Application<br />
  8. 8. WEB Application<br />An Application accessed over a network.<br />Runs inside a Web Browser<br />
  9. 9. What is PHP?<br />PHP is a web development language <br />PHP: Hypertext Preprocessor<br />PHP6 <br />Created by RasmusLerdorf in 1995<br />
  10. 10. Why PHP?<br />Open Source<br />Free<br />Very Good set of built-in functions<br />Very Good Database support<br />Great support Community<br />Large number of Frameworks<br />
  11. 11. Getting Started with PHP<br />
  12. 12. Basic PHP Program<br />Index.php<br /><HTML><br /><HEAD><br /><TITLE>My first PHP program</TITLE><br /></HEAD><br /><BODY><br /><?php<br /> print("Hello, World<BR />n");<br />phpinfo();<br />?><br /></BODY><br /></HTML><br />
  13. 13. Including files<br />Separate Client and Server code <br /><?php<br />include('/filepath/filename')<br />require('/filepath/filename')<br />include_once('/filepath/filename')<br />require_once('/filepath/filename')<br />?><br />
  14. 14. PHP SYNTAX<br />PHP Is Forgiving<br />HTML Is Not PHP<br />PHP’s Syntax Is C-Like<br />PHP is whitespace insensitive<br />PHP is sometimes case sensitive<br />Statements are expressions terminated by semicolons<br />Braces make blocks<br />
  15. 15. PHP Is Forgiving<br />Focuses on convenience for the programmer<br />Doesn’t focus that much on correctness<br />
  16. 16. HTML Is Not PHP<br />PHP Syntax applies only within <br />PHP should not be confused with HTML<br /><?php<br />?><br />
  17. 17. PHP’s Syntax Is C-Like<br />PHP is whitespace insensitive<br /><?php<br />$four=2+2;// single spaces<br />$four=2+2;// spaces and tabs<br />$four=<br />2<br />+<br />2;// multiple lines<br />?><br />
  18. 18. PHP’s Syntax Is C-Like<br />PHP is sometimes case sensitive<br /><?php<br />$capital=67;<br />print("Variable capital is $capital<BR>");<br />print("Variable CaPiTaL is $CaPiTaL<BR>");<br />?><br />OUTPUT :<br />Variable capital is 67Notice: Undefined variable: CaPiTaL in D:xampphtdocsphpindex.php on line 5Variable CaPiTaL is <br />
  19. 19. PHP’s Syntax Is C-Like<br />PHP is sometimes case sensitive<br /><?php<br /> HELLO();<br /> function hello()<br /> {<br />PrINT("hello");<br /> }<br /> ?><br />Output: <br /> hello<br />
  20. 20. PHP’s Syntax Is C-Like<br />Statements are expressions terminated by semicolons<br /><?php<br />$greeting="Welcome to PHP!";<br />?><br />
  21. 21. Expressions and types<br />$value=2+2*"nonsense"+TRUE;<br />Output : 3<br />
  22. 22. Braces make blocks<br /><?php<br />if(3==2+1)<br />print("Good Morning have a nice day<BR>");<br />if(3==2+1)<br />{<br />print("Good Morning ");<br />print("have a nice day<BR>");<br />}<br />?><br />
  23. 23. Comments<br /><?php<br />/* This is<br /> a comment in<br /> PHP */<br /># This is a comment, and<br />Not a Comment # this is the second line of the comment<br /> Not a Comment<br />// This is a comment too. <br />?><br />
  24. 24. Variables<br />All variables in PHP are denoted with a leading dollar sign ($).<br />The value of a variable is the value of its most recent assignment. <br />Variables are assigned with the = operator, with the variable on the left-hand side and the expression to be evaluated on the right.<br />Variables can, but do not need, to be declared before assignment. <br />Variables have no intrinsic type other than the type of their current value.<br />Variables used before they are assigned have default values.<br />
  25. 25. Variables<br />PHP variables are Perl-like<br />All variables in PHP start with a leading $ sign<br />Variable names must be composed of<br />letters (uppercase or lowercase)<br />digits (0–9)<br />underscore characters ( _ )<br />The first character after the $ may not be a number<br />
  26. 26. Variables<br />Declaring variables (or not)<br />Assigning variables<br />$pi=3+0.14159; // Double<br />$pi="3 + 0.14159"; // String<br />Reassigning variables<br />$my_num_var="This should be a number – hope it’s reassigned";<br />$my_num_var=5;<br />Unassigned variables<br />
  27. 27. Variable scope<br />You can switch modes if you want<br /><HTML><br /><HEAD><br /><?php<br />$username="Jane Q. User";<br />?><br /></HEAD><br /><BODY><br /><?php<br />print("$username<BR>");<br />?><br /></BODY><br /></HTML><br />Output :<br />Jane Q. User<br />
  28. 28. Types in PHP: Don’t Worry, Be Happy<br />No variable type declarations<br />$first_number=55.5;<br />$second_number="Not a number at all";<br />Automatic type conversion<br />$pi=3+0.14159;<br />Types assigned by context<br />print(substr(12345,1,2));<br />Output : 23<br />
  29. 29. Type Summary<br />Integers are whole numbers, without a decimal point, like 495.<br />Doubles are floating-point numbers, like 3.14159or49.0.<br />Booleans have only two possible values: TRUE and FALSE.<br />NULL is a special type that only has one value: NULL .<br />Strings are sequences of characters, like <br /> 'PHP 4.0 supports string operations.'<br />
  30. 30. Type Summary<br />Arrays are named and indexed collections of other values.<br />Objects are instances of programmer-defined classes, which can package up both other kinds of values and functions that are specific to the class.<br />Resources are special variables that hold references to resources external to PHP (such as database connections).<br />
  31. 31. Outputting<br />Echo and print<br />Basic constructs of the PHP language<br />
  32. 32. Echo<br />echo"This will print in the user’s browser window.";<br />echo("This will print in the user’s browser window.");<br />Both are equivalent.<br />Multiple parameters can be used<br />echo"This will print in the ","user’s browser window.";<br />But not like this<br />echo("This will produce a ","PARSE ERROR!");<br />
  33. 33. Print<br />The command print is very similar to echo, with two important differences:<br />Unlike echo, printcan accept only one argument.<br />Unlike echo, printreturns a value, which represents whether or not the printstatement succeeded.<br />print("3.14159");// print a string<br />print(3.14159);// print a number<br />
  34. 34. Variables and strings<br />$animal="antelope";<br />$animal_heads=1;<br />$animal_legs=4;<br />print("The $animal has $animal_heads head(s).<BR>");<br />print("The $animal has $animal_legs leg(s).<BR>");<br />Output<br />The antelope has 1 head(s).<br />The antelope has 4 leg(s).<br />
  35. 35. PHP Control Structures and Functions<br />Topics <br />Boolean Expressions<br />Branching<br />Looping<br />Terminating Execution<br />Using Functions<br />
  36. 36. Boolean Expressions<br />Boolean constants<br />if(TRUE)<br />print("This will always print<BR>");<br />else<br />print("This will never print<BR>");<br />
  37. 37. Logical operators<br />
  38. 38. Comparison operators<br />
  39. 39. Branching<br />If-else<br />if($first>$second)<br />{<br />$difference=$first-$second;<br />print("The difference is $difference<BR>");<br />}<br />else<br />{<br />$difference=$second-$first;<br />print("The difference is $difference<BR>");<br />}<br />
  40. 40. Branching<br />Switch<br />$day=5;<br />switch($day)<br />{<br />case1:echo"Sunday";break;<br />case2:echo"Monday";break;<br />case3:echo"Tuesday";break;<br />case4:echo"Wednesday";break;<br />case5:echo"Thursday";break;<br />case6:echo"Friday";break;<br />case7:echo"Saturday";break;<br />default:echo"Not a day";break;<br />}<br />
  41. 41. Looping<br />While<br />$count=1;<br />while($count<=10)<br />{<br />print("count is $count<BR>");<br />$count=$count+1;<br />}<br />
  42. 42. Looping<br />Do-while<br />$count=45;<br />do<br />{<br />print("count is $count<BR>");<br />$count=$count+1;<br />}<br />while($count<=10);<br />
  43. 43. Looping<br />For<br />$limit=5;<br />for($count=0;$count<$limit;$count=$count+1)<br />print"hello <br>";<br />
  44. 44. Using Functions<br />functionfunction-name($argument-1,$argument-2,..)<br />{<br /> statement-1;<br /> statement-2;<br />...<br />}<br />
  45. 45. Functions<br /><?php<br />sayhello("Welcome To PHP");<br />functionsayhello($hello_string)<br />{<br />print$hello_string;<br />}<br />?><br />
  46. 46. Functions with return-values<br /><?php<br />$sum=add(5,3);<br />echo"5+3=",$sum;<br />function add($a,$b)<br />{<br />return$a+$b;<br />}<br />?><br />
  47. 47. HTTP Requests<br />HTTP Is Stateless<br />GET Arguments<br />POST Arguments<br />
  48. 48. GET Request<br />
  49. 49. GET Request<br /><html><br /><head><br /><title>Http Get Request </title><br /></head><br /><body><br /><formmethod="GET"action="get.php"><br /> Name:<inputtype="text"name="Name"/><br><br /> College : <inputtype="text"name="College"/><br><br /><inputtype="submit"/><br /></form><br /></body><br /></html><br />
  50. 50. GET Request<br />
  51. 51. GET Request<br /><?php<br />echo"Hello ",$_GET['Name'];<br />echo'<br>';<br />echo'from ',$_GET['College'];<br />?><br />
  52. 52. POST Request<br />
  53. 53. POST Request<br /><html><br /><head><br /><title>Http Get Request </title><br /></head><br /><body><br /><formmethod="POST"action="get.php"><br /> Name:<inputtype="text"name="Name"/><br><br /> College : <inputtype="text"name="College"/><br><br /><inputtype="submit"/><br /></form><br /></body><br /></html><br />
  54. 54. POST Request<br />
  55. 55. POST Request<br /><?php<br />echo"Hello ",$_POST['Name'];<br />echo'<br>';<br />echo'from ',$_POST['College'];<br />?><br />
  56. 56. mySQL Database Connectivity<br />
  57. 57.
  58. 58.
  59. 59.
  60. 60.
  61. 61.
  62. 62. <?php<br />$db_username="phpuser";<br />$db_password="phppassword";<br />$db="phpdb";<br />$db_host="localhost";<br />$db_table="login";<br />$conn=mysql_connect($db_host,$db_username,$db_password)ordie(mysql_error());<br />mysql_select_db($db);<br />$query="Select * from $db_table";<br />$result=mysql_query($query,$conn);<br />while($array=mysql_fetch_array($result))<br />{<br />for($i=0;$i<3;$i++)<br />{<br />echo$array[$i].' ';<br />}<br />echo'<br>';<br />}<br />?><br />
  63. 63. PHP File Handling<br />
  64. 64. PHP File Handling<br />FileForm.php<br /><html><br /><body><br /><formmethod="POST"action="file.php"><br />Name:<inputtype="text"name="name"/><br /></form><br />Names : <br><br /><?php<br />$file=fopen("welcome.txt","r+");<br />while(! feof( $file ) )<br />{<br />echofgets($file) .'<br>';<br />}<br />?><br /></body><br /></html><br />
  65. 65. PHP File Handling<br /><?php<br />$file=fopen("welcome.txt","a+");<br />$name=$_POST['name'];<br />fwrite($file,$name.'<br />');<br />header("location:fileform.php");<br />?><br />
  66. 66. PHP Arrays<br />Creating Arrays<br />$fruit_basket=array('apple','orange','banana','pear');<br />OR<br />$fruit_basket[0]='apple';<br />$fruit_basket[1]='orange';<br />$fruit_basket[]='banana';<br />$fruit_basket[]='pear';<br />
  67. 67. PHP Arrays<br />$fruit_basket=array(0=>'apple',1=>'orange',<br />2=>'banana',3=>'pear');<br />$fruit_basket=array('red'=>'apple',<br />'orange'=>'orange‘,'yellow'=>'banana','green'=>'pear');<br />
  68. 68. PHP Arrays<br />echo$fruit_basket['yellow'];<br />Multidimensional Arrays<br />$cornucopia=array('fruit'=><br />array('red'=>'apple',<br />'orange'=>'orange',<br />'yellow'=>'banana',<br />'green'=>'pear'),<br />'flower'=><br />array('red'=>'rose',<br />'yellow'=>'sunflower',<br />'purple'=>'iris'));<br />
  69. 69. PHP Arrays Deleting<br />Deleting from Arrays<br />$my_array[0]='wanted';<br />$my_array[1]='unwanted';<br />$my_array[2]='wanted again';<br />unset($my_array[1]);<br />Unset is not the same as<br />$my_array[1]='';<br />
  70. 70. PHP Arrays Iteration<br /><?php<br />$cornucopia=array('fruit'=>array('red'=>'apple',<br />'orange'=>'orange',<br />'yellow'=>'banana',<br />'green'=>'pear'),<br />'flower'=>array('red'=>'rose',<br />'yellow'=>'sunflower',<br />'purple'=>'iris'));<br />foreach($cornucopiaas$cor)<br />{<br />foreach($coras$c)<br />{<br />echo$c.'<br>';<br />}<br />}<br />?><br />
  71. 71. Randomness<br /><html><br /><head><br /><?php<br />$hex=array('a','b','c','d','e','f',1,2,3,4,5,6,7,8,9,0);<br />$color='';<br />for($i=0;$i<6;$i++)<br />{<br />$random=mt_rand(0,sizeof($hex)-1);<br />$color=$color.$hex[$random];<br />}<br />?><br /><style><br /> body<br /> {<br /> background-color:<?phpecho$color;?>;<br /> }<br /></style><br /></head><br /></html><br />
  72. 72. Session<br />A PHP session variable is used to store information about, or change settings for a user session. <br />Session variables hold information about one single user, and are available to all pages in one application.<br />
  73. 73. Working with Sessions<br />Session.php<br /><?php<br />session_start();<br />if(!isset($_SESSION['username']))<br />{<br />header('location:login.php');<br />}<br />echo'Hello '.$_SESSION['username'];<br />session_destroy();<br />?><br />
  74. 74. Sessions<br />Login.php<br /><?php<br />if(!isset($_POST['uname']))<br />{<br />echo"<form method='POST' action='' ><br /> username:<input type='text' name='uname' /><br /> </form>";<br />exit();<br />}<br />session_start();<br />$uname=$_POST['uname'];<br />$_SESSION['username']=$uname;<br />header("location:session.php");<br />?><br />
  75. 75. Cookies<br />Login.php<br /><?php<br />if(!isset($_POST['uname']))<br />{<br />echo"<form method='POST' action='' ><br /> username:<input type='text' name='uname' /><br /> </form>";<br />exit();<br />}<br />$uname=$_POST['uname'];<br />setcookie('username',$uname);<br />header("location:cookie.php");<br />?><br />
  76. 76. Cookies<br />Cookie.php<br /><?php<br />if(!isset($_COOKIE['username']))<br />{<br />header('location:login.php');<br />}<br />echo'Hello '.$_COOKIE['username'];<br />setcookie('username','');<br />?><br />
  77. 77. A simple web application<br />Database Schema<br />
  78. 78. Index.php<br /><?php<br />session_start();<br />if(!isset($_SESSION['uid']))<br /> header('location:login.html');<br />else<br /> header('location:main.php');<br />?><br />
  79. 79. Login.html<br /><h1>Login</h1><br /><formmethod="POST"action="login.php"><br />Username:<inputtype="text"name="USERNAME"/><br><br />Password:<inputtype="password"name="PASSWORD"/><br><br /><inputtype="submit"value="login"/><br /></form><br><br /><ahref="register.html">Register</a><br />
  80. 80. Login.php<br /><?php<br />$conn=mysql_connect('localhost','phpuser','phppassword')ordie(mysql_error());<br />mysql_select_db('userdata')ordie(mysql_error());<br />$username=$_POST['USERNAME'];<br />$password=$_POST['PASSWORD'];<br />$password=sha1($password);<br />$result=mysql_query("Select * from login where username='$username' and password='$password' ;",$conn)ordie(mysql_error());<br />if(!mysql_num_rows($result))<br />{<br />echo"Invalid Username / Password<br>Please go back !";<br />exit();<br />}<br />$row=mysql_fetch_array($result)ordie(mysql_error());<br />session_start();<br />$_SESSION['uid']=$row['uid'];<br />$_SESSION['username']=$row['username'];<br />header('location:main.php');<br />?><br />
  81. 81. Register.html<br /><h1>Register</h1><br /><formmethod="POST"action="register.php"><br />Username:<inputtype="text"name="USERNAME"/><br><br />Password:<inputtype="password"name="PASSWORD"/><br><br /><inputtype="submit"value=“register"/><br /></form><br><br /><ahref="login.html">Login</a><br />
  82. 82. Register.php<br /><?php<br />session_start();<br />session_destroy();<br />$conn=mysql_connect('localhost','phpuser','phppassword')ordie(mysql_error());<br />mysql_select_db('userdata')ordie(mysql_error());<br />$username=$_POST['USERNAME'];<br />$password=$_POST['PASSWORD'];<br />$password=sha1($password);<br />$result=mysql_query("Select * from login where username='$username' ;",$conn)ordie(mysql_error());<br />if(mysql_num_rows($result))<br />{<br />echo"Username Already in use <br>Please go back !";<br />exit();<br />}<br />mysql_query("Insert into login values(null,'$username','$password');",$conn)ordie(mysql_error());<br />header('location:login.html');<br />?><br />
  83. 83. Main.php<br /><?php<br />session_start();<br />if(!isset($_SESSION['uid']))<br /> header('location:login.html');<br />?><br /><h1>Main page</h1><br /><h2>Hello <?phpecho$_SESSION['username'];?></h2><br /><h3><ahref="logout.php">Logout</a></h3><br /><formaction="add.php"method="POST"><br /> Data :<inputtype="text"name="data"/><inputtype="submit"value="Add Data"/><br /></form><br /><h3>Your Data:</h3><br />
  84. 84. Main.php<br /><?php<br />$conn=mysql_connect('localhost','phpuser','phppassword')ordie(mysql_error());<br />mysql_select_db('userdata')ordie(mysql_error());<br />$result=mysql_query("select * from data where uid=".$_SESSION['uid']." ;",$conn);<br />if(!mysql_num_rows($result))<br />exit();<br />echo'Did uid data<br>';<br />while($row=mysql_fetch_array($result))<br />{<br />for($i=0;$i<3;$i++)<br />{<br />echo$row[$i].' ';<br />}<br />echo'<br>';<br />}<br />?><br />
  85. 85. Add.php<br /><?php<br />session_start();<br />if(!isset($_SESSION['uid']))<br /> header('location:login.html');<br />$conn=mysql_connect('localhost','phpuser','phppassword')ordie(mysql_error());<br />mysql_select_db('userdata')ordie(mysql_error());<br />if(!isset($_POST['data']))<br /> header('location:main.php');<br />$uid=$_SESSION['uid'];<br />$data=$_POST['data'];<br />mysql_query("insert into data values(null,$uid,'$data');",$conn);<br />header('location:main.php');<br />?><br />
  86. 86. Logout.php<br /><?php<br />session_start();<br />session_destroy();<br />header('location:index.php');<br />?><br />
  87. 87. bibliography<br />PHP6 and MySQL bible by Steve Suehring, Tim Converse, and Joyce Park<br />W3schools.com<br />Php.net<br />
  88. 88. Presentation<br />This presentation is available online at http://j.mp/ki0GlO<br />

×