Using URL Variables

One of the most common methods of state management is the use of a URL variable for the unique identifier. In fact, most state-management mechanisms use cookies, URL variables, or some combination of the two.

In order to use a URL variable to identify the client, you must make sure that the HTML generated from your application uses this variable in all URLs that the user will use to navigate your site. Most Web applications will use one of two primary methods of navigation—form submission buttons and links—so I will focus on these approaches.

For links, the HTML you need to output is something like the following:

<a href=”/example.php?unique_id=12345”>Click Here</a> 

Although the only dynamic information in this example is the value of unique_id, it might be a good approach to store the entire query string in a variable to ease future alterations. If the query string separator character (?) is also part of this value being stored, an empty (or NULL) value of your query string variable will not taint the appearance of your URLs when appended by adding an unnecessary query string separator. For example, consider the following sample PHP:

<? 
$unique_id = “12345”; 
if (isset($unique_id)) 
{ 
    $query_string = “?unique_id=$unique_id”; 
} 
else 
{ 
    $query_string = “”; 
} 
?> 
<a href=”/example.php<? echo $query_string; ?>”>Click Here</a> 

This script outputs the HTML link given in the previous example.

In a more realistic situation, the value of unique_id would be obtained from the client (for a returning client) or generated by the script (for a new client), so it would not be manually set to 12345 as in this example.

For form submissions, the approach is similar. Consider the following PHP:

<form action=”/example.php<? echo $query_string; ?>” method=”post”> 

If this code is executed later in the same script as the code from the previous example, the following will be output:

<form action=”/example.php?unique_id=12345” method=”post”> 

The rest of the form can remain unaltered. The Web browser will use the POST method when the user submits the form and will include the unique identifier in the URL of the requested page.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.9.12