Jump to content
xisto Community
FaLgoR

Complete Login System With PHP + MYSQL

Recommended Posts

Dear FaLgoR,Nice sharing of web development tutorial, i hope that many loving of web development will get big advantages from yoututorials thanks again and again for a such good tutorial.Thanks

Share this post


Link to post
Share on other sites

This tutorial is not so concerned about teaching people. This is strictly because you didn't provide information on how we can learn from this. And perhaps you could do this way.Firstly... lets ...Then we can...Try it out before we move on!Ok, now lets...alright, your nearly there...Anyway, thanks for this 'so-so' tutorial. Continue to work hard!

Share this post


Link to post
Share on other sites

Thanks a lot! I've learned something new from it, since I need some small parts for my own user system. :)

Share this post


Link to post
Share on other sites

if its ur own.. u dont need any parts :)

It's just some codes, that I am missing to make mine work.Not that I am stealing from it, but I am learning by reading. :(

Share this post


Link to post
Share on other sites

Its an complete login sistem made and tested by me and I think itwill be very usefull for people who are tryn to learn PHP.
First, let's make register.php:

<?include("conn.php"); // create a file with all the database connectionsif($do_register){ // if the submit button were clickedif((!$name) || (!$email) || (!$age) || (!$login) || (!$password) || (!$password2)){print "You can't let any fields in blank.\n"; // if the user did not put some fieldexit;}$name = stripslashes($name);$email = stripslashes($email);$age = stripslashes($age);$login = stripslashes($login);$password = stripslashes($password);$password2 = stripslashes($password2);// this is for security reasonsif($password != $password2){ // if passwords didn't matchprint "The password and the confirmation are not the same!\n";exit;}$password = md5($password);mysql_query("INSERT INTO table (name,email,age,login,password) VALUES ('$name','$email',$age,'$login','$password')") or die (mysql_error());print "Done!\n"; // if its okay, show this messageexit;} // close the first "if"?><form action="register.php" method="post">Name: <input type="text" name="name"><br>Email: <input type="text" name="email"><br>Age: <input type="text" name="age"><br>Login: <input type="text" name="login"><br>Password: <input type="password" name="password"><br>Password Again: <input type="password" name="password2"><br><input type="submit" name="do_register" value="Sumbit"></form>

And now 'conn.php', which is 'included' in the above file.

$host = 'localhost';$user = 'root';$pass = '';$db = 'yourdb';mysql_connect($host,$user,$pass) or die ("Database is unavaiable. Please try again later.");mysql_select_db($db) or die ("Database is unavaiable. Please try again later.");

Notice from jlhaslip:

I have cut and pasted the missing 'conn.php' in here to avoid all the confusion about it having been missed in the original version of the tutorial.
Most of the following posts concern this out-of-place file, so this note might help explain why they are there.


And now, login.php:

<?include("conn.php");if($do_login){$login = stripslashes($login); // VERY IMPORTANT FOR SECURITY OF YOUR DATABASE DON'T ERASE IT$passwd = stripslashes($passwd); // VERY IMPORTANT FOR SECURITY OF YOUR DATABASE DON'T ERASE IT$check = mysql_query("SELECT * FROM table WHERE login='$login' LIMIT 1;");$user = mysql_fetch_array($check);if($user[password] == md5($passwd)){ // if the writed password and the db password are the same...setcookie("login","$login",time()+360000);setcookie("pass","$passwd",time()+360000);// ...set the cookies...header("Location: userspage.php"); // ...and redirect to restrict page}else{print "Login or password incorrects!\n";exit;}}?><form action="login.php" method="post">Login: <input type="text" name="login"><br>Passwd: <input type="password" name="passwd"><input type="submit" name="do_login" value="Log-in!"></form>
And finally, userspage.php:

<?if(isset($HTTP_COOKIE_VARS["login"])){?>Page contents here<?}else{?>This page is restrict for registered users only!<?}?>

verify.php:
<?include("conn.php"); // include page with the database connection$cookie = $HTTP_COOKIE_VARS; // to reduce the var's name :o)if($cookie[login] && $cookie[pass]){$login = $cookie[login];$pass = $cookie[pass];$usrquery = mysql_query("SELECT * FROM members WHERE nick='$login' AND password='$pass';") or die (mysql_error()); // search for the user$user = mysql_fetch_array($usrquery);if($user[level] != 'Admin') header("Location: notfound.htm"); // if the user is not an admin, redirect to an error page}?>

admin.php:
<?include("verify.php"); // it will verify if the user is an admin?><!-- Here, the table with all the members --><table width="100%" border="0" cellspacing="0" cellpadding="0">    <tr>      <td>        <form method="post" action="members.php">          <table width="100%" border="0" cellspacing="3" cellpadding="0">            <tr bgcolor="#333333">              <th width="6%" class="header"><font size="1">Editar</font></th>              <th width="1%" class="header"><font size="1">ID</font></th>              <th width="24%" class="header"><font size="1">Name</font></th>              <th width="13%" class="header"><font size="1">Age</font></th>              <th width="40%" class="header"><font size="1">E-Mail</font></th>              <th width="11%" class="header"><font size="1">Details...</font></th>            </tr><?$query = mysql_query("SELECT * FROM members ORDER BY id;");if(!mysql_fetch_array($query)) // If there is no membersprint "<tr><td align=\"center\" colspan=\"7\"><font color=\"#FFFFFF\" size=\"2\"><b>Sorry, there is no members registered.</b></font></td></tr>\n";// Show you a messagewhile($profiles = mysql_fetch_array($query)){?>            <tr bgcolor="#666666">              <td> <div align="center"><input type="checkbox" name="id[]" value="<?=$profiles[id]?>"></div></td>              <td> <div align="center"><?=$profiles[id]?></div></td>              <td> <div align="center"><?=$profiles[name]?></div></td>              <td> <div align="center"><?=$profiles[age]?></div></td>              <td> <div align="center"><?=$profiles[email]?></div></td>              <td> <div align="center"><a href="profiles.php?op=edit&id=<?=$profiles[id]?>" target="_blank">More info...</a></div></td>            </tr><?}?>          </table>        </td>    </tr>  </table></form>
Done, now, profiles.php (used to see and edit member information):
<?include("verify.php"); // always put this page, or everybody would have access to this pagefunction Update (&$member, $table, $data){    global $id;    $items = explode(" ",$data);	$update = "";	$i = 0;	while ($tmp = $items[$i++])	{  $data = $member[$tmp];  if (is_numeric($data))  	$update .= "$tmp=$data";  else  {        sqlQuotes($data);  	$update .= "$tmp='$data'";        }  if ($items[$i]) $update .= ",";	}	mysql_query("UPDATE $table SET $update WHERE id=$member[id];");}// this function is really nice!!switch($op){case 'edit': // if you're trying to edit/see info$profile = mysql_fetch_array(mysql_query("SELECT * FROM members WHERE id=$id;")); // save the user informations on an variable?><!-- now, lets show an table -->  <form action="profiles.php?op=doedit&memberid=<?=$profile[id]?>" method="post">    <table width="100%" border="0" cellspacing="3" cellpadding="0">      <tr>         <td width="25%"><font color="#FFFFFF">ID</font></td>        <td width="75%"><input name="id" type="text" id="id" value="<?=$profile[id]?>" size="2"></td>      </tr>      <tr>         <td><font color="#FFFFFF">Name</font></td>        <td><input name="name" type="text" id="nome" value="<?=$profile[name]?>" maxlength="32"></td>      </tr>      <tr>         <td><font color="#FFFFFF">Age</font></td>        <td><input name="age" type="text" value="<?=$profile[age]?>" maxlength="32"></td>      </tr>      <tr>         <td><font color="#FFFFFF">Country</font></td>        <td><input name="country" type="text" id="estado" value="<?=$profile[country]?>" size="2" maxlength="2"></td>      </tr>      <tr>         <td><font color="#FFFFFF">City</font></td>        <td><input name="city" type="text" id="cidade" value="<?=$profile[city]?>"></td>      </tr>      <tr>         <td><font color="#FFFFFF">ICQ</font></td>        <td><input name="icq" type="text" id="icq" value="<?=$profile[icq]?>"></td>      </tr>      <tr>         <td height="22"><font color="#FFFFFF">MSN</font></td>        <td><input name="msn" type="text" id="msn" value="<?=$profile[msn]?>"></td>      </tr>      <tr>         <td><font color="#FFFFFF">HP</font></td>        <td><input name="hp" type="text" id="hp" value="<?=$profile[hp]?>" size="40"></td>      </tr>      <tr>         <td><font color="#FFFFFF">E-mail</font></td>        <td><input name="email" type="text" id="email" value="<?=$profile[email]?>" maxlength="60"></td>      </tr>      <tr>         <td colspan="2"> </td>      </tr>      <tr>         <td colspan="2"><div align="center">             <input type="submit" value="Save">                          <input type="reset" value="Reset">          </div></td>      </tr>    </table>  </form><?break;case 'doedit':if(!$memberid)return;$profile[name] = $name;$profile[age] = $age;$profile[country] = $country;$profile[city] = $city;$profile[icq] = $icq;$profile[msn] = $msn;$profile[hp] = $hp;$profile[email] = $email;Update($profile,"members","name age country city icq msn hp email");mysql_query("UPDATE members SET id=$id WHERE id=$memberid;"); // update user's idEndNow("Details saved!<br><br><a href=\"admin.php\">Back</a>");break;}?>

Try to don't only copy the code and post into your site. If you do it, you will learn nothing with this tut. I hope it have been usefull for you! :)

hey FaLgoR can i ask you how good you are with php? I need to put together something for a client and i was wondering if i could ask you for some help, not very good at php yet. thanks if you can't its cool just wondering.

Share this post


Link to post
Share on other sites
MySQL codeComplete Login System

I've tried to find out the mysql code, but can't find out how to make it work. Just found out so it will put the information into the table when somebody registers, but I can't login, and when I go to the admin page, there is no members.

-question by Freestyle

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.