View Full Version : How To Make User Pages Tut


PunkGo
09-14-2003, 05:40 AM
hey, im bored so imma show u how to make ur very own user/login thingy with php

step 1. make a register page...

<html>
<h1>Register Here</h1>
<br><br>
<strong>Username</strong>
<form method="post" action="register_check.php">
<input type="text" name="username"><br><br>
<strong>Password</strong>
<input type="password" name="password"><br><br>
<input type="submit" value="Submit">
<input type="reset" value="Clear">
</form>
</html>

ok...les explain...everyone shood know the html, for the action i put register_check.php so all this info entered on this page will goto the register_check.php
ok, off to register_check.php......

<?php
session_start();
mysql_connect("localhost", "username", "password")
or die(mysql_error());

mysql_select_db("dbname")
or die(mysql_error());

$username = $HTTP_POST_VARS['username'];
$passwd = $HTTP_POST_VARS['password'];

$query = "SELECT * FROM tablename WHERE username='$username'";
$result = mysql_query($query);
$rows = mysql_num_rows($result);

if($rows > 0){
echo "Username ";
echo $username;
echo " Is Taken, Please Try Again!";
}
else {
mysql_query("INSERT INTO tablename (username, password)
VALUES('$username', '$passwd');
echo 'Sucessfully Registered!';
}
?>


thats all im writing for now...if u want me to continue this tutorial...reply to this..

PunkGo
09-24-2003, 10:22 PM
ok guys, part 2:
someone sent me a PM asking me to continue, thx, so here it goes:
we made the register pages, now to make login page..
--Login.htm----
<html>
<head>
<title>Login Page</title>
</head>
<body>
<h1>Login Page</h1>
<strong>Username:</strong><br>
<form action="logincheck.php" method="post">
<input type="text" name="username"><br><br>
<strong>Password:</strong><br>
<input type="password" name="password">
</body>
</html>
---END----
ok, we made a basic script...we made the aciton goto logincheck which we are gonna look over in a sec..then amde 2 fields, username n password...where login checks to see if they match..

--Logincheck.php----

<?php
mysql_connect("localhost", "username", "password");
or die(mysql_error());

mysql_select_db("dbname");
or die(mysql_error());

$username = $_POST['username'];
$passwd = $_POST['password'];
$result = mysql_query("SELECT * FROM tablename WHERE `username`='$username' AND password='$passwd'");
$row = mysql_num_rows($result);

if($row > 0){
$_SESSION['valid_user'] = $username;
echo 'Login Successful!<br>';
echo '<a href="http://www.blah.com/members.php">Member's Section!</a>';
}
else {
echo 'Sorry, You Entered Wrong Username/Password';
}
?>


ok, les break it down...the connect's connect to the DB where the register page stored it... the $username = $_POST['username']; is what the user entered on the login.htm page. for username, then $passwd = $_POST['password']; is what the entered on the login page for their password....the $result finds the username in the DB and see's if their password is correct, if it is...it will say login seccessful then give the link of a members page, the $row uses mysql_num_rows, to find the rows with the username/password combo... the if($row > 0){
sees if their IS a username/password combo and they entered it correctly then it will display the successful login stuff, now, the $_SESSION['valid_user'] = $username; sets a session that will echo the person's username through the whole site...its really handy ecspeccaill for membs section, ok on to members section!

---members.php------

<?php
mysql_connect("localhost", "username", "password");
or die(mysql_error());

mysql_select_db("dbname");
or die(mysql_error());

global $HTTP_SESSION_VARS;
$username = $_SESSION['valid_user'];

if(isset($username)){
echo 'Welcome To The Members Section, ' . $username . '!';
}
else {
echo 'You Are Not Supposed To Be Here!';
}
?>

ok, the connects connects to db, the global $HTTP_SESSION_VARS; i dunno what that does, lol but i used it one time,.....so i always use it now, the $username = $_SESSION['valid_user'];
calls for that session we made earlyer and gets is, then the if(isset($username)){
sees if the user session has been made, if it has it will display the welcome message, if not it will display the 'go away' message, lol hope this helps people, gimme a IM on AIM my screenname is PunkGo ,
later,
-PunkGo