tecmeister Posted January 22, 2008 Share Posted January 22, 2008 Hi everyone, Im new to php. I would like to know how to create a login register page. Like the register page on this website. I have look at some toturials, but they either have login or register. Would some please be able to show me a tutorial or give me advice on how to do it. Thankyou very much for the help that you be able to give me. Quote Link to comment https://forums.phpfreaks.com/topic/87278-login-register-script/ Share on other sites More sharing options...
marklarah Posted January 22, 2008 Share Posted January 22, 2008 If you give me 5 mins, ill let you have my script. Ok? Quote Link to comment https://forums.phpfreaks.com/topic/87278-login-register-script/#findComment-446416 Share on other sites More sharing options...
marklarah Posted January 22, 2008 Share Posted January 22, 2008 Ok, this is my script. Obviously, look at the fields and modify them. Login: //put this before your opening html tags <?php ob_start(); session_start(); include 'dbs.php'; if (isset($_POST['username']) && isset($_POST['password'])) { //if the user has just tried to log in $username = $_POST['username']; $password = $_POST['password']; $query= 'select * from users ' ."where username='$username' " ."and password=md5('$password')"; $result = mysql_query($query); if (mysql_num_rows($result) > 0) { //if they are in the db, register the user id $_SESSION['user'] = $username; $userr = mysql_query("SELECT * FROM users WHERE username = '$username'"); $row = mysql_fetch_assoc($userr); $_SESSION['lvl'] = intval($row['level']); $_SESSION['userID'] = intval($row['ID']); $id = $_SESSION['userID']; $lin = "UPDATE users SET llin = NOW() WHERE ID = '$id' "; mysql_query($lin) or die('Error, query failed now'); $liyn = "UPDATE users SET active = '1' WHERE ID = '$id' "; mysql_query($liyn) or die('Error, query failed'); } } ?> // Put this stuff in the middle of your page. <font face="Verdana, Helvetica, Trebuchet MS, Comic Sans MS, Arial"> <?php if (isset($_SESSION['user'])) { //logged in OK echo '<br>Welcome back, '.$_SESSION['user'].'!'.' Redirecting in 5 seconds or <a href="index.php">continue</a>.'; ?> <meta http-equiv="REFRESH" content="5;url=index.php"> <?php } else { if (isset($username)) { //they tried to log in and failed echo '<font color="grey"><i>Login failed. Please try again.</i></font><br><br>'; } else { //they have not tried to log in or have not yet logged out echo 'If you already have a user name and password, please login below. <br /><br>'; } //provide a form to log in echo '<form method="post" action="login.php">'; echo '<table>'; echo '<tr><td>User Name:</td>'; echo '<td><input type="text" name="username"></td></tr>'; echo '<tr><td>Password:</td>'; echo '<td><input type="password" name="password"></td></tr>'; echo '<tr><td colspan="2" align="center">'; echo '<input type="submit" value="Log in"></td></tr>'; echo '</table></form>'; echo '<font size="-3">Not registered? Register <a href="register.php">here</a>.</font>'; } ob_end_flush(); ?> </font> Register: <font face="Verdana, Helvetica, Trebuchet MS, Comic Sans MS, Arial"> <form method="post" action="reging.php"> WARNING! You MUST type in a valid Email address otherwise you will not be able to make changes to your account. <hr> Type Username: <input type="text" name="u1"> <br> Retype Username: <input type="text" name="u2"> <br> <br> Type Password: <input type="password" name="p1"> <br> Retype Password: <input type="password" name="p2"> <br> <br> Type Email: <input type="text" name="e1"> <br> Retype Email: <input type="text" name="e2"> <br> <br> <input type="submit" value="Submit"><input type="reset" value="Reset"> </form> </font> reging.php: <?php include 'dbs.php'; if ($_POST['u1'] == $_POST['u2'] && $_POST['e1'] == $_POST['e2'] && $_POST['p1'] == $_POST['p2']) { if ($_POST['u1'] == ''){ echo "No."; exit; } if ($_POST['e1'] == ''){ echo "No."; exit; } if ($_POST['p1'] == ''){ echo "No."; exit; } $sql = "select * from users where username='" . $_POST['u1'] . "'"; $result = mysql_query($sql); $sql2 = "select * from users where email='" . $_POST['e1'] . "'"; $result2 = mysql_query($sql2); if (mysql_num_rows($result) >= 1) { $err = "Username, "; }else{ $err = ""; } if (mysql_num_rows($result2) >= 1) { $err2 = "Email "; } else { $err2 = ""; } if (mysql_num_rows($result) >= 1 || mysql_num_rows($result2) >= 1) { echo '<b>Error: </b>'; echo $err; echo $err2; echo 'already in use - <a href="register.php">try again</a>.'; } else { $username = $_POST['u1']; $pass = md5($_POST['p1']); $email = $_POST['e1']; $usernamee = htmlentities($username); $emaile = htmlentities($email); $sql = "INSERT INTO users (ID, username, password, email, level) VALUES (NULL, '$usernamee', '$pass', '$emaile', '10')"; mysql_query($sql) or die('Registration failed'); ?> <meta http-equiv="REFRESH" content="0;url=pregister.php"> <?php } }else{ echo '<b>Error:</b> You filled the form in incorrectly - <a href="register.php">try again</a>.'; } ?> then for a protected page: if(!isset($_SESSION['user'])) { ?> You have to login/register this page. Return to the <a href="forum.php">General Board List</a> to select a board. <?php exit; } // put protected page here ?> Okay, thats quick. If some stuff doesn't link up, tell me, ill fix it. Where it includes dbs.php, thats your database connection page Any problems, questions, let me know. Quote Link to comment https://forums.phpfreaks.com/topic/87278-login-register-script/#findComment-446420 Share on other sites More sharing options...
revraz Posted January 22, 2008 Share Posted January 22, 2008 Or you can learn how to do it http://www.roscripts.com/PHP_login_script-143.html Quote Link to comment https://forums.phpfreaks.com/topic/87278-login-register-script/#findComment-446429 Share on other sites More sharing options...
tecmeister Posted January 22, 2008 Author Share Posted January 22, 2008 Or you can learn how to do it http://www.roscripts.com/PHP_login_script-143.html Thanks i will take a look at the tutoial. There is one part of the tutorial that i dont understand. What do i call this php <?php function checkLogin($levels) { if(!$_SESSION['logged_in']) { $access = FALSE; } else { $kt = split(' ', $levels); $query = mysql_query('SELECT Level_access FROM users WHERE ID = "'.mysql_real_escape_string($_SESSION['user_id']).'"'); $row = mysql_fetch_assoc($query); $access = FALSE; while(list($key,$val)=each($kt)) { if($val==$row['Level_access']) {//if the user level matches one of the allowed levels $access = TRUE; } } } if($access==FALSE) { header("Location: login.php"); } else { //do nothing: continue } } ?> Thanks for the help. Quote Link to comment https://forums.phpfreaks.com/topic/87278-login-register-script/#findComment-446468 Share on other sites More sharing options...
Ken2k7 Posted January 22, 2008 Share Posted January 22, 2008 What do i call this php What do you mean what do you call it? Quote Link to comment https://forums.phpfreaks.com/topic/87278-login-register-script/#findComment-446482 Share on other sites More sharing options...
tecmeister Posted January 23, 2008 Author Share Posted January 23, 2008 What do i call this php What do you mean what do you call it? Im sorry i thought that i might need to give it a name by saving it. Im very sorrt about it. Quote Link to comment https://forums.phpfreaks.com/topic/87278-login-register-script/#findComment-446521 Share on other sites More sharing options...
legohead6 Posted January 23, 2008 Share Posted January 23, 2008 if you are new to php start with simpler stuff, first start with form handling, and then database management, then once you figure out those 2, creating a login should be fairly straight forward. Quote Link to comment https://forums.phpfreaks.com/topic/87278-login-register-script/#findComment-446665 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.