Home arrow PHP arrow Page 3 - Developing a User Personalization System with PHP and Cookies

User Login - PHP

Making your site more appealing to repeat traffic is always a priority among web developers. However users of your site are likely to be coming to your site for different reasons. This tutorial will teach you how to use PHP and cookies to make your site more user-customizable and therefore more likely to attract repeat users.

TABLE OF CONTENTS:
  1. Developing a User Personalization System with PHP and Cookies
  2. Grabbing Headlines
  3. User Login
  4. Reading from Cookies
  5. Conclusion
By: Duncan Lamb
Rating: starstarstarstarstar / 6
September 20, 1999

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement
Now a form should be built to enter information, make choices, and then enter the data in the database. Here's a simple form with a few more choices available:


<HTML> <HEAD> <TITLE>Login now!</TITLE> </HEAD> <BODY> <br>Choose the headlines you wouldlike to see!<br> <form method=post action="newuser.php3"> <br> Please pick a login name:<input name="login" type="TEXT" value=""> <br> Now pick a password:<input name="password" type="TEXT" value=""> <br><br> <br>News Choice 1:<SELECT NAME="news1" size=5 value=""> <option value="slashdot.lnk">Slashdot <option value="freshmeat.lnk">Freshmeat <option value="voodooextreme.lnk">VoodooExtreme <option value="devshed.lnk">DevShed <option value="bluesnews.lnk">Blue's News </select> <br>News Choice 2:<SELECT NAME="news2" size=5 value=""> <option value="slashdot.lnk">Slashdot <option value="freshmeat.lnk">Freshmeat <option value="voodooextreme.lnk">VoodooExtreme <option value="devshed.lnk">DevShed <option value="bluesnews.lnk">Blue's News </select> <br>News Choice 3:<SELECT NAME="news3" size=5 value=""> <option value="slashdot.lnk">Slashdot <option value="freshmeat.lnk">Freshmeat <option value="voodooextreme.lnk">VoodooExtreme <option value="devshed.lnk">DevShed <option value="bluesnews.lnk">Blue's News </select> <br><input type="submit" name="Submit" value=" Submit "> <br></FORM> </BODY> </HTML>

The action in the form is "newuser.php3." Once data has been entered in the form, this script inserts a new row of data in the database.


<? SetCookie("visitor",$login,time()+864000); ?> <HTML> <HEAD><TITLE>Thanks!</TITLE></HEAD> <BODY> <? mysql_connect("localhost", "username", "pass") or DIE("Unable to connect to database"); @mysql_select_db("project") or die("Unable to select database"); $result = mysql_query("Select * from users where login='$login'"); if(!mysql_numrows($result)) { #Make a new login - entire page follows $result = mysql_query("insert into users values ( '$login', '$password', NOW(), '$news1', '$news2', '$news3')"); ?> <br> Thanks for logging in!<br><br> To see what the page looks like with your personal settings, please <a href="index.php3">return</a>. <? } else { echo "Whoa! Someone has already picked that username!<br><br>"; echo "Please hit the back button and pick a unique login."; } ?> </body> </html>

Notice that "Setcookie" command? We'll talk about on the next page. For now, notice how the database is checked for duplicate logins first (lines 10), then a new row is created (line 12). Afterwards, a link to the start page is given.

At the end of the script, the user gets some feedback if his login is not unique.

Normally to pass variables to any sort of script, variables have to be added to the end of the URL, but what we want is a simple solution, so whenever a user comes to the page, their preferences will be displayed without any mumbo jumbo at the end of the url. We still need a way to pass our scripts information about the user. The best way to do so is with cookies, and nothing handles cookies as easily as PHP3.



 
 
>>> More PHP Articles          >>> More By Duncan Lamb
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PHP ARTICLES

- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: