Free file hosting, free video sharing

<<< The Web Hosting >>>


Go Back   Noeman GSM > Webhosting & Website development > Website development > Programming

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 06-30-2007, 11:12 AM
Spiral's Avatar
CO-Admin
 
Join Date: Sep 2004
Location: Martil
Age: 25
Posts: 16,762
Reputation: 29697
Spiral Mega Super MemberSpiral Mega Super MemberSpiral Mega Super MemberSpiral Mega Super MemberSpiral Mega Super MemberSpiral Mega Super MemberSpiral Mega Super MemberSpiral Mega Super MemberSpiral Mega Super MemberSpiral Mega Super MemberSpiral Mega Super Member
Default User-aware PHP scripts

One of the top qualities of PHP is it's dynamics. That means that everything about a webpage can be changed based on certain elements, either user-supplied or system-based. You can base the output of a webpage roughly on anything the user's browser sends, like the user's resolution. This tutorial is going to cover the general area of making a PHP script user-aware. Basically, that stands for the fact that the webpage is designed for more than one user's preferences.

User management can be either based on username/password pairs, in which case authentication mechanisms have to be built, this method being recommended for private-content webpages; the other method of obtaining user-aware scripts is by using cookies. We'll start by explaining cookie-based systems first, being the easiest to understand. Further in this tutorial you'll find some information on the first type of user-aware scripts - username/password protected areas.

PHP has built-in cookie setting and control functions, easily adaptable to any condition. The following bit of code is a script that checks for a cookie, and based on the result, outputs the page's background in a different color:

Code:
<? 
if(isset($HTTP_COOKIE_VARS["bgcolor"])) $mycolor = $HTTP_COOKIE_VARS["bgcolor"];  
                // if the cookie exists, set the variable to its contents  
else $mycolor = darkblue; // and if it doesn't, use the default option  
echo "<body bgcolor=$mycolor><B>The background is of color $mycolor!</B>";  
                // finally, echo the final output  
?>

We should note that $HTTP_COOKIE_VARS["bgcolor"] could have been replaced with $bgcolor, but if we had done that, we could have been sure whether this variable would have been set actually in a cookie or in a GET request (like accesing the script like this: script.php?bgcolor=deeppurple). In the same fashion, $HTTP_GET_VARS and $HTTP_POST_VARS are available.

Here is the setcookie() script:

PHP Code:
<? 
if(isset($cookieval)) setcookie("bgcolor"$cookievaltime()+600);  
                
// if the value to be assigned to the cookie has been sent, assign the cookie  
                // make the cookie expire in one hour  
else {        // if the value hasn't been sent, print a self-referencing form that sends it  
        
echo "<form action="$PHP_SELF" method=POST>n";  
        echo 
"<input type=text name=cookieval value="#FFFFFF">n";  
        
echo "</form>n";  
        }  
?>
Now, when you normally parse a user-sent variable you should always pass it through addslashes(). This makes sure the user doesn't try to "hijack" the HTML output. But in this case we don't need that, since the user could only "hijack" his own HTML output, since the cookie is not available to anyone but himself.

Now, we've only scratched the surface of what control cookies can bring to your website. For example, someone could write a function that orders paragraphs differently for each user, making sure that a user interested in a certain field always gets a paragraph about that first. This can also apply to news! Any news on the topic of hardware shown on the site will always be shown on the main page for users that have specified hardware as an interest.

The most common use for cookies is to store login names and passwords. You may build a full content management system, with a MySQL backbone, storing user interests and preferences, and by retrieving preferences from the database the script could order or change the page elements.

Basically, having a user-aware script using username/password authentication has only one downside. While much simpler cookie auth is completely unstable and cannot be used for any serious authentication, it's very easy to apply to the user. A user just selects what he wants from a list, and doesn't have to worry about it until the cookie expires, is cleared or lost, while username/password pair authentication usually requires a long process of registration which takes longer and usually not enjoyed by users, plus the user has to remember his password. Think what happens when a user signs up for 30 or so of these websites, every time with a different password of course, so as not to compromise his other accounts if one of the passwords gets leaked. You try remembering 30 passwords!

Thanks for Skee
Reply With Quote
Sponsored Links
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


All times are GMT. The time now is 09:28 PM.


Powered by vBulletin® Version 3.7.2
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0
Copyright © 2004-2008 Noeman . All rights reserved
Credit Card | Personal Loans | Strokes | Moissanite Jewelry For Sale | Free Ringtones

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114