Unformatted text preview:

More PHPWhat we'll cover• A short history of php• Parsing• Variables• Arrays• Operators• Control Structures• Forms• Functions• Accessing the shell• Sockets• Regular ExpressionsForms• Obviously, one of the things you need tohandle are forms• You can make an html only form, and submitthat to a php file• You can also make a single php file thatmakes the form, and submit that to itself, andon the submit, present a different page, andthen after a confirmation present a third page,and so on, and so on….• We'll talk about bothSimple Formsimpleform/simpleformsimpleform/simpleform.html.html<form action=".<form action="./simpleform/simpleform..php" php" method="GET" name="choices">method="GET" name="choices"><p>What is the meaning of life?<<p>What is the meaning of life?</p/p>><p><<p><textarea textarea rows="1" cols="50" wrap="virtual"rows="1" cols="50" wrap="virtual" name = "answer"><name = "answer"></textarea/textarea><></p/p>><p>What is your name?<<p>What is your name?</p/p>><p><<p><textarea textarea rows="1" cols="50" wrap="virtual"rows="1" cols="50" wrap="virtual" name = "name"><name = "name"></textarea/textarea><></p/p>><p>Where is your car?<<p>Where is your car?</p/p>><p><<p><textarea textarea rows="1" cols="50" wrap="virtual"rows="1" cols="50" wrap="virtual" name = "car"><name = "car"></textarea/textarea><></p/p>><p><input type="submit" value="Submit" align="middle" /><<p><input type="submit" value="Submit" align="middle" /></p/p>></form></form>Simple Form phpsimpleform/simpleformsimpleform/simpleform..phpphp<?<?phpphpif (count($_GET) < 1) if (count($_GET) < 1) { { echo "no echo "no GETsGETs!";!"; } }else else { { echo "Here is the GET array:"; echo "Here is the GET array:"; echo "<pre>"; echo "<pre>"; print_rprint_r($_GET);($_GET); echo "</pre>"; echo "</pre>"; } }?>?>• The php that catches theform reads the GET array• All this one does is usedprint_r() to show the results• Can you think of anyadvantages ordisadvantages to using twoseparate pages this way?• What about the firstcondition? When is it met?Simple form to itself• To do all of this with one php file, weneed a way to detect where we are inthe process• So on first load, we present the form• On second load, after submitting theform, we process results…simpleform_self.php<?<?phpphpif (!$_GET) if (!$_GET) {{ echo "<form action=\"" . $_SERVER["PHP_SELF"] . "\" method=\"GET\" echo "<form action=\"" . $_SERVER["PHP_SELF"] . "\" method=\"GET\" name=\"choices\"> name=\"choices\">\n"\n";; echo "<p>What is the meaning of life?<echo "<p>What is the meaning of life?</p/p>> <p><<p><textarea textarea rows=\"1\" cols=\"50\" wrap=\"virtual\" rows=\"1\" cols=\"50\" wrap=\"virtual\" name = \"answer\">< name = \"answer\"></textarea/textarea><></p/p>>\n"\n";; echo "<p>What is your name?<echo "<p>What is your name?</p/p>> <p><<p><textarea textarea rows=\"1\" cols=\"50\" wrap=\"virtual\" rows=\"1\" cols=\"50\" wrap=\"virtual\" name = \"name\"><name = \"name\"></textarea/textarea><></p/p>>\n"\n";; echo "<p>Where is your car?<echo "<p>Where is your car?</p/p>> <p>< <p><textarea textarea rows=\"1\" cols=\"50\" wrap=\"virtual\" rows=\"1\" cols=\"50\" wrap=\"virtual\" name = \"car\">< name = \"car\"></textarea/textarea><></p/p>>\n"\n";; echo '<input type="hidden" value = "111" name = echo '<input type="hidden" value = "111" name = "key_number" "key_number" />' ;/>' ; echo '<p><input type="submit" value="Submit" align="middle" /><echo '<p><input type="submit" value="Submit" align="middle" /></p/p>'; >'; } }simpleform_self.phpelse else { { echo "Here is the GET array:"; echo "Here is the GET array:"; echo "<pre>"; echo "<pre>"; print_rprint_r($_GET);($_GET); echo "</pre>"; echo "</pre>"; } }?>?></form></form></body></body></html></html>• When the form issubmitted, the url has aGET array• So it passes past the ifstatement, and lands onthe else, where weprocess data• Again, we're justprint_r()'ing the results• There's an error here, canyou spot it?Sessions• A very useful aspect to php is the abilityto maintain session data• To do this, you create a session, thenload it with data• The session data is stored server side,usually in /etc, and is keyed (but notencrypted), and remains for that url andremote ipsimpleform_self_session.php• Sessions need to be created before anyhtml headers are sent, so do this at thevery top of the page• Use session_register() to create slots forvariables• After these variables are populated, youcan access them via the $_SESSION array• In this example we'll define a key (randomlygenerated) and a counter (to track steps)simpleform_self_session.php<?php// Start a session, and set a key and a counter// A session is a method of tracking state for a given // browser session, and allows storage of data on the serversession_start(); // create a sessionsession_register('session_key'); // register a session var for a key to // track this session // this is done to help detect reloadssession_register('session_counter'); // session counter is used to // track where we are in // a session?>// if the session_key is not yet set, generate a random number for the key// and set the session_counter to 0 (this situation indicates that the form// is being loaded for the first time)if (!$_SESSION['session_key']) { $_SESSION['session_key'] = rand(); $_SESSION['counter'] = 0; }simpleform_self_session.php• Now, we can check see if the key is set,if not, we're on a first load and wegenerate a key and set the counter to0….// If we don't have // If we don't have key_numberkey_number, then the user hasn't filled in the form, then the user hasn't filled in the formif (!$_GET[if (!$_GET["key_number""key_number"])]) { { echo "<form action=\"" . $_SERVER["PHP_SELF"] . "\" method=\"GET\"echo "<form action=\"" . $_SERVER["PHP_SELF"] . "\" method=\"GET\" name=\"choices\"> name=\"choices\">\n"\n";; echo "<p>What is the meaning of life?<echo "<p>What is the meaning of life?</p/p><p><><p><textarea textarea rows=\"1\"rows=\"1\" cols=\"50\" wrap=\"virtual\" name = \"answer\">< cols=\"50\" wrap=\"virtual\" name = \"answer\"></textarea/textarea><></p/p>>\n"\n";; …… echo '<input type="hidden" value =


View Full Document

UNC-Chapel Hill INLS 672 - More PHP

Download More PHP
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view More PHP and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view More PHP 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?