Developing a User Personalization System with PHP and Cookies - User Login
(Page 3 of 5 )
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.
Next: Reading from Cookies >>
More PHP Articles
More By Duncan Lamb