Developing State-enabled Applications With PHP

Written by John L


Continued from page 1

Installment 3

PHP Session Handling - Cookies Enabled

Instead of storing session information atrepparttar browser throughrepparttar 105569 use of cookies,repparttar 105570 information can instead be stored atrepparttar 105571 server in session files. One session file is created and maintained for each user session. For example, if there are three concurrent users browsingrepparttar 105572 website, three session files will be created and maintained - one for each user. The session files are deleted ifrepparttar 105573 session is explicitly closed byrepparttar 105574 PHP script or by a daemon garbage collection process provided by PHP. Good programming practice would call for sessions to be closed explicitly inrepparttar 105575 script.

The following is a typical server-browser sequence of events that occur when a PHP session handling is used: 1. The server knows that it needs to rememberrepparttar 105576 State of browsing session 2. PHP generates a sssion ID and creates a session file to store future information as required by subsequent pages 3. A cookie is generated wihrepparttar 105577 session ID atrepparttar 105578 browser 4. This cookie that storesrepparttar 105579 session ID is transparently and automatically sent torepparttar 105580 server for all subsequent requests torepparttar 105581 server

The following PHP session-handling example accomplishesrepparttar 105582 same outcome asrepparttar 105583 previous cookie example. Copyrepparttar 105584 code below (bothrepparttar 105585 php andrepparttar 105586 html) into a file withrepparttar 105587 .php extension and test it out.

[?php //starts a session session_start();

//informs PHP that count information needs to be remembered inrepparttar 105588 session file if (!session_is_registered("count")) { session_register("count"); $count = 0; } else { $count++; }

$session_id = session_id(); ?]

[html] [head] [title]PHP Session Handling - Cookie-Enabled[/title] [/head] [body] The current session id is: [?=$session_id ?] This page has been displayed: [?=$count ?] times. [/body] [/html]

A summary ofrepparttar 105589 functions that PHP provides for session handling are: 1. boolean start_session() - initializes a session 2. string session_id([string id]) - either returnsrepparttar 105590 current session id or specifyrepparttar 105591 session id to be used whenrepparttar 105592 session is created 3. boolean session_register(mixed name [, mixed ...]) - registers variables to be stored inrepparttar 105593 session file. Each parameter passed inrepparttar 105594 function is a separate variable 4. boolean session_is_registered(string variable_name) - checks if a variable has been previously registered to be stored inrepparttar 105595 session file 5. session_unregister(string varriable_name) - unregisters a variable fromrepparttar 105596 session file. Unregistered variables are no longer valid for reference inrepparttar 105597 session. 6. session_unset() - unsets all session variables. It is important to note that allrepparttar 105598 variables remain registered. 7. boolean session_destroy() - destroysrepparttar 105599 session. This is opposite ofrepparttar 105600 start_session function.

The next installment discusses how to manage sessions using PHP session handling functions when cookies are disabled...

Installment 4

PHP Session Handling - Without Cookies

If cookies are disabled atrepparttar 105601 browser,repparttar 105602 above example cannot work. This is because althoughrepparttar 105603 session file that stores allrepparttar 105604 variables is kept atrepparttar 105605 server, a cookie is still needed atrepparttar 105606 browser to storerepparttar 105607 session ID that is used to identifyrepparttar 105608 session and its associated session file. The most common way around this would be to explicitly passrepparttar 105609 session ID back torepparttar 105610 server fromrepparttar 105611 browser as a query parameter inrepparttar 105612 URL.

For example,repparttar 105613 PHP script generates requests subsequent torepparttar 105614 start_session call inrepparttar 105615 following format: http://www.yourhost.com/yourphpfile.php?PHPSESSID=[actual session ID]

The following are excerpts that illustraterepparttar 105616 discussion:

Manually buildingrepparttar 105617 URL: $url = "http://www.yoursite.com/yourphppage.php?PHPSESSID=" . session_id(); [a href="[?=$url ?]" rel="nofollow"]Anchor Text[/a]

Buildingrepparttar 105618 URL using SID: [a href="http://www.yoursite.com/yourphppage.php?[?=SID ?]" rel="nofollow"]Anchor Text[/a]

This PHP scripting article is written by John L. John L is the Webmaster of The Ultimate BMW Blog! (http://www.bimmercenter.com).

The Ultimate BMW Blog!


MySQL Database Handling in PHP

Written by John L


Continued from page 1

The @ operator is used to suppress any error messages that mysql_connect() and mysql_select_db() functions may produce if an error occurred. The die() function is used to endrepparttar script execution and display a custom error message.

Executing SQL Statements against a MySQL database

Oncerepparttar 105568 connection and database selection is successfully performed,repparttar 105569 PHP script can now proceed to operate onrepparttar 105570 database using standard SQL statements. The mysql_query() function is used for executing standard SQL statements againstrepparttar 105571 database. Inrepparttar 105572 following example,repparttar 105573 PHP script queries a table called tbl_login inrepparttar 105574 previously selected database to determine if a username/password pair provided byrepparttar 105575 user is valid.

Assumption: The tbl_login table has 3 columns named login, password, last_logged_in. The last_logged_in column storesrepparttar 105576 time thatrepparttar 105577 user last logged intorepparttar 105578 system.

// The $username and $passwd variable should rightly be set byrepparttar 105579 login form // throughrepparttar 105580 POST method. Forrepparttar 105581 purpose of this example, we’re manually coding it. $username = “john”; $passwd = “mypassword”;

// We generate a SELECT SQL statement for execution. $sql="SELECT * FROM tbl_login WHERE login = '".$username."' AND password = '".$passwd."'";

// Executerepparttar 105582 SQL statement againstrepparttar 105583 currently selected database. // The results will be stored inrepparttar 105584 $r variable. $r = mysql_query($sql);

// Afterrepparttar 105585 mysql_query() command executes,repparttar 105586 $r variable is examined to // determine ofrepparttar 105587 mysql_query() was successfully executed. if(!$r) { $err=mysql_error(); print $err; exit(); }

// If everything went well, check ifrepparttar 105588 query returned a result – i.e. ifrepparttar 105589 username/password // pair was found inrepparttar 105590 database. The mysql_affected_rows() function is used for this purpose. // mysql_affected_rows() will returnrepparttar 105591 number of rows inrepparttar 105592 database table that was affected // byrepparttar 105593 last query if(mysql_affected_rows()==0){ print "Username/password pair is invalid. Please try again."; } else {

// If successful, read outrepparttar 105594 last logged in time into a $last variable for display torepparttar 105595 user $row=mysql_fetch_array($r); $last=$row["last_logged_in"]; print “Login successful. You last logged in at ”.$last.”.”;

}

The above example demonstrated how a SELECT SQL statement is executed againstrepparttar 105596 selected database. The same method is used to execute other SQL statements (e.g. UPDATE, INSERT, DELETE, etc.) againstrepparttar 105597 database usingrepparttar 105598 mysql_query() and mysql_affected_rows() functions.

This PHP scripting article is written by John L. John L is the Webmaster of The Ultimate BMW Blog! (http://www.bimmercenter.com).

The Ultimate BMW Blog!


    <Back to Page 1
 
ImproveHomeLife.com © 2005
Terms of Use