batstanggt Posted July 4, 2011 Share Posted July 4, 2011 Hey Guys, Im new on here so bear with me. I am currently on about day 5 (and yes I mean full days almost) trying to get a php login/register/logout script to work. I know thats pretty sad but its where I'm at lol. Anyways I have seen numerous tutorials online and none of them seem to be working. Long story short does anyone have a tutorial or resource they know of that actually works. If anyone has a template of a script for what I am trying to accomplish that would be great as well. Im not by any means looking for an easy way out but Ive started from scratch about 4 or 5 times now so maybe starting with a script that any of you could provide may help you help me work through this issue ? Hahah anyways guys any help at all would be greatly appreciated. I know your going to probably ask me to see the script I'm using but as I say I have used about 4 now and I dont really know which one to post. Id rather use one that more educated people such as yourselves can recomend and work through that as I'm sure in trying to resolve this I've butchered the scripts to shreds anyways. If nobody has a good script to use then I will post it, Ill do anything to get this to work lol. Im going crazy! -SB Quote Link to comment https://forums.phpfreaks.com/topic/241038-cant-find-a-single-login-script-tutorial-that-works/ Share on other sites More sharing options...
PFMaBiSmAd Posted July 4, 2011 Share Posted July 4, 2011 If you have had no success with any script, it would be better to pick the one you feel most comfortable with and troubleshoot why it is not working. You may have some fundamental issue on your server/development system that would prevent every suggested script from working. Short answer: In programming, it is aways better to troubleshoot and find the root cause of a problem then to waste a lot of time just trying things and throwing away code simply because it doesn't work. Pick the script you understand the best and post it along with a statement of the symptoms and any errors you got when you tried it. Quote Link to comment https://forums.phpfreaks.com/topic/241038-cant-find-a-single-login-script-tutorial-that-works/#findComment-1238085 Share on other sites More sharing options...
batstanggt Posted July 4, 2011 Author Share Posted July 4, 2011 First of all thank you for the reply. I appreciate you taking your time to help me. Second here is the script that i have hacked up the least if at all (cant remember anymore lol). -SB Quote Link to comment https://forums.phpfreaks.com/topic/241038-cant-find-a-single-login-script-tutorial-that-works/#findComment-1238086 Share on other sites More sharing options...
batstanggt Posted July 4, 2011 Author Share Posted July 4, 2011 <? session_start(); include("database.php"); ?> <? /** * Returns true if the username has been taken * by another user, false otherwise. */ function usernameTaken($username){ global $conn; if(!get_magic_quotes_gpc()){ $username = addslashes($username); } $q = "select username from users where username = '$username'"; $result = mysql_query($q,$conn); return (mysql_numrows($result) > 0); } /** * Inserts the given (username, password) pair * into the database. Returns true on success, * false otherwise. */ function addNewUser($username, $password){ global $conn; $q = "INSERT INTO users VALUES ('$username', '$password')"; return mysql_query($q,$conn); } /** * Displays the appropriate message to the user * after the registration attempt. It displays a * success or failure status depending on a * session variable set during registration. */ function displayStatus(){ $uname = $_SESSION['reguname']; if($_SESSION['regresult']){ ?> <h1>Registered!</h1> <p>Thank you <b><? echo $uname; ?></b>, your information has been added to the database, you may now <a href="main.php" title="Login">log in</a>.</p> <? } else{ ?> <h1>Registration Failed</h1> <p>We're sorry, but an error has occurred and your registration for the username <b><? echo $uname; ?></b>, could not be completed.<br> Please try again at a later time.</p> <? } unset($_SESSION['reguname']); unset($_SESSION['registered']); unset($_SESSION['regresult']); } if(isset($_SESSION['registered'])){ /** * This is the page that will be displayed after the * registration has been attempted. */ ?> <html> <title>Registration Page</title> <body> <? displayStatus(); ?> </body> </html> <? return; } /** * Determines whether or not to show to sign-up form * based on whether the form has been submitted, if it * has, check the database for consistency and create * the new account. */ if(isset($_POST['subjoin'])){ /* Make sure all fields were entered */ if(!$_POST['user'] || !$_POST['pass']){ die('You didn\'t fill in a required field.'); } /* Spruce up username, check length */ $_POST['user'] = trim($_POST['user']); if(strlen($_POST['user']) > 30){ die("Sorry, the username is longer than 30 characters, please shorten it."); } /* Check if username is already in use */ if(usernameTaken($_POST['user'])){ $use = $_POST['user']; die("Sorry, the username: <strong>$use</strong> is already taken, please pick another one."); } /* Add the new account to the database */ $md5pass = md5($_POST['pass']); $_SESSION['reguname'] = $_POST['user']; $_SESSION['regresult'] = addNewUser($_POST['user'], $md5pass); $_SESSION['registered'] = true; echo "<meta http-equiv=\"Refresh\" content=\"0;url=$HTTP_SERVER_VARS[php_SELF]\">"; return; } else{ /** * This is the page with the sign-up form, the names * of the input fields are important and should not * be changed. */ ?> <html> <title>Registration Page</title> <body> <h1>Register</h1> <form action="<? echo $HTTP_SERVER_VARS['PHP_SELF']; ?>" method="post"> <table align="left" border="0" cellspacing="0" cellpadding="3"> <tr><td>Username:</td><td><input type="text" name="user" maxlength="30"></td></tr> <tr><td>Password:</td><td><input type="password" name="pass" maxlength="32"></td></tr> <tr><td colspan="2" align="right"><input type="submit" name="subjoin" value="Join!"></td></tr> </table> </form> </body> </html> <? } ?> MOD EDIT: code tags added. Quote Link to comment https://forums.phpfreaks.com/topic/241038-cant-find-a-single-login-script-tutorial-that-works/#findComment-1238087 Share on other sites More sharing options...
Nuv Posted July 4, 2011 Share Posted July 4, 2011 Do you have database.php in place too ? What errors is this code giving ? Quote Link to comment https://forums.phpfreaks.com/topic/241038-cant-find-a-single-login-script-tutorial-that-works/#findComment-1238089 Share on other sites More sharing options...
QuickOldCar Posted July 4, 2011 Share Posted July 4, 2011 Is that from here? http://www.evolt.org/node/60265 Read the comments and will most likely find the answer. Quote Link to comment https://forums.phpfreaks.com/topic/241038-cant-find-a-single-login-script-tutorial-that-works/#findComment-1238090 Share on other sites More sharing options...
batstanggt Posted July 4, 2011 Author Share Posted July 4, 2011 yea i do have database.php in place : <? /** * Connect to the mysql database. */ $conn = mysql_connect("localhost", "mysqllogin", "mysqlpassword") or die(mysql_error()); mysql_select_db('databasename', $conn) or die(mysql_error()); ?> Here it is for your perusal if that helps at all. It basically just keeps coming up saying Register Failed. I think I going to see that in my sleep.... Quote Link to comment https://forums.phpfreaks.com/topic/241038-cant-find-a-single-login-script-tutorial-that-works/#findComment-1238092 Share on other sites More sharing options...
batstanggt Posted July 4, 2011 Author Share Posted July 4, 2011 Thats where i got the script from in fact ... I read every comment and couldnt find anything wrong... Quote Link to comment https://forums.phpfreaks.com/topic/241038-cant-find-a-single-login-script-tutorial-that-works/#findComment-1238093 Share on other sites More sharing options...
EdwinPaul Posted July 4, 2011 Share Posted July 4, 2011 If you put those two lines on top of your script, you may see more (error-)messages: ini_set('display_errors',1); error_reporting(E_ALL | E_STRICT); Quote Link to comment https://forums.phpfreaks.com/topic/241038-cant-find-a-single-login-script-tutorial-that-works/#findComment-1238106 Share on other sites More sharing options...
batstanggt Posted July 4, 2011 Author Share Posted July 4, 2011 hey i tried putting those two lines at the top of the register.php file and now when I click register on my index.php file in the browser (which links to my register page). The register page wont load with that at the top. Anything else? I seem to be the only person online who can't figure out a login/register system. Oh wait! I just looked where the url usually is in the browser and it says that there is an undefined variable. Here is the direct quote: notice: Undefined variable: HTTP_SERVER_VARS in /var/www/register.php on line 126 Thanks guys for your help so far. Quote Link to comment https://forums.phpfreaks.com/topic/241038-cant-find-a-single-login-script-tutorial-that-works/#findComment-1238218 Share on other sites More sharing options...
EdwinPaul Posted July 4, 2011 Share Posted July 4, 2011 hey i tried putting those two lines at the top of the register.php file and now when I click register on my index.php file in the browser (which links to my register page). The register page wont load with that at the top. Anything else? I seem to be the only person online who can't figure out a login/register system. Oh wait! I just looked where the url usually is in the browser and it says that there is an undefined variable. Here is the direct quote: notice: Undefined variable: HTTP_SERVER_VARS in /var/www/register.php on line 126 Thanks guys for your help so far. Change the next line: <form action="<? echo $HTTP_SERVER_VARS['PHP_SELF']; ?>" method="post"> To: <form action="" method="post"> Quote Link to comment https://forums.phpfreaks.com/topic/241038-cant-find-a-single-login-script-tutorial-that-works/#findComment-1238226 Share on other sites More sharing options...
batstanggt Posted July 4, 2011 Author Share Posted July 4, 2011 Ok so let me get this straight Edwin. I take out the first line u posted and replace it with the second line you posted? Or do I put the second As well as the first? I assume 1 or the other but Im just asking to be on the safe side. Thank you -SB Quote Link to comment https://forums.phpfreaks.com/topic/241038-cant-find-a-single-login-script-tutorial-that-works/#findComment-1238257 Share on other sites More sharing options...
Pikachu2000 Posted July 4, 2011 Share Posted July 4, 2011 When posting code, enclose it within the forum's . . . BBCode tags. Quote Link to comment https://forums.phpfreaks.com/topic/241038-cant-find-a-single-login-script-tutorial-that-works/#findComment-1238258 Share on other sites More sharing options...
jcbones Posted July 4, 2011 Share Posted July 4, 2011 I take out the first line u posted and replace it with the second line you posted? YES! Quote Link to comment https://forums.phpfreaks.com/topic/241038-cant-find-a-single-login-script-tutorial-that-works/#findComment-1238262 Share on other sites More sharing options...
batstanggt Posted July 4, 2011 Author Share Posted July 4, 2011 Sorry Pikachu2000 thats my bad ill use the brackets next time I post one like I said im new to the forum. But it has been duely noted and wont happen again. Ok so I've switched those two lines and wont be able to test if its working for another 4 hours yet (Im at work the suspense is killing me) when I get home so I can connect to my server (as Im hosting this on a home server for now and see if it works. I'm sure you guys will be hearing from me shortly and I'll let you know if that solved my issue or not. Once again I cant express how thankful I am for everyones help so far. -SB Quote Link to comment https://forums.phpfreaks.com/topic/241038-cant-find-a-single-login-script-tutorial-that-works/#findComment-1238273 Share on other sites More sharing options...
batstanggt Posted July 4, 2011 Author Share Posted July 4, 2011 Alright, bad news didnt work. Still says registration failed etc... It isnt giving that error code in the address bar anymore however so we might be getting warmer. Anymore ideas? Quote Link to comment https://forums.phpfreaks.com/topic/241038-cant-find-a-single-login-script-tutorial-that-works/#findComment-1238345 Share on other sites More sharing options...
Pikachu2000 Posted July 4, 2011 Share Posted July 4, 2011 Post the revised code. Quote Link to comment https://forums.phpfreaks.com/topic/241038-cant-find-a-single-login-script-tutorial-that-works/#findComment-1238348 Share on other sites More sharing options...
batstanggt Posted July 4, 2011 Author Share Posted July 4, 2011 Here we are <? ini_set('display_errors',1); error_reporting(E_ALL | E_STRICT);//this was added after the fact so that i can see what errors session_start(); include("database.php"); ?> <? /** * Returns true if the username has been taken * by another user, false otherwise. */ function usernameTaken($username){ global $conn; if(!get_magic_quotes_gpc()){ $username = addslashes($username); } $q = "select username from users where username = '$username'"; $result = mysql_query($q,$conn); return (mysql_numrows($result) > 0); } /** * Inserts the given (username, password) pair * into the database. Returns true on success, * false otherwise. */ function addNewUser($username, $password){ global $conn; $q = "INSERT INTO users VALUES ('$username', '$password')"; return mysql_query($q,$conn); } /** * Displays the appropriate message to the user * after the registration attempt. It displays a * success or failure status depending on a * session variable set during registration. */ function displayStatus(){ $uname = $_SESSION['reguname']; if($_SESSION['regresult']){ ?> <h1>Registered!</h1> <p>Thank you <b><? echo $uname; ?></b>, your information has been added to the database, you may now <a href="main.php" title="Login">log in</a>.</p> <? } else{ ?> <h1>Registration Failed</h1> <p>We're sorry, but an error has occurred and your registration for the username <b><? echo $uname; ?></b>, could not be completed.<br> Please try again at a later time.</p> <? } unset($_SESSION['reguname']); unset($_SESSION['registered']); unset($_SESSION['regresult']); } if(isset($_SESSION['registered'])){ /** * This is the page that will be displayed after the * registration has been attempted. */ ?> <html> <title>Registration Page</title> <body> <? displayStatus(); ?> </body> </html> <? return; } /** * Determines whether or not to show to sign-up form * based on whether the form has been submitted, if it * has, check the database for consistency and create * the new account. */ if(isset($_POST['subjoin'])){ /* Make sure all fields were entered */ if(!$_POST['user'] || !$_POST['pass']){ die('You didn\'t fill in a required field.'); } /* Spruce up username, check length */ $_POST['user'] = trim($_POST['user']); if(strlen($_POST['user']) > 30){ die("Sorry, the username is longer than 30 characters, please shorten it."); } /* Check if username is already in use */ if(usernameTaken($_POST['user'])){ $use = $_POST['user']; die("Sorry, the username: <strong>$use</strong> is already taken, please pick another one."); } /* Add the new account to the database */ $md5pass = md5($_POST['pass']); $_SESSION['reguname'] = $_POST['user']; $_SESSION['regresult'] = addNewUser($_POST['user'], $md5pass); $_SESSION['registered'] = true; echo "<meta http-equiv=\"Refresh\" content=\"0;url=$HTTP_SERVER_VARS[php_SELF]\">"; return; } else{ /** * This is the page with the sign-up form, the names * of the input fields are important and should not * be changed. */ ?> <html> <title>Registration Page</title> <body> <h1>Register</h1> <form action="" method="post"> <table align="left" border="0" cellspacing="0" cellpadding="3"> <tr><td>Username:</td><td><input type="text" name="user" maxlength="30"></td></tr> <tr><td>Password:</td><td><input type="password" name="pass" maxlength="32"></td></tr> <tr><td colspan="2" align="right"><input type="submit" name="subjoin" value="Join!"></td></tr> </table> </form> </body> </html> <? } ?> Quote Link to comment https://forums.phpfreaks.com/topic/241038-cant-find-a-single-login-script-tutorial-that-works/#findComment-1238351 Share on other sites More sharing options...
batstanggt Posted July 4, 2011 Author Share Posted July 4, 2011 Anymore ideas? Quote Link to comment https://forums.phpfreaks.com/topic/241038-cant-find-a-single-login-script-tutorial-that-works/#findComment-1238384 Share on other sites More sharing options...
batstanggt Posted July 5, 2011 Author Share Posted July 5, 2011 nothing?!?! Quote Link to comment https://forums.phpfreaks.com/topic/241038-cant-find-a-single-login-script-tutorial-that-works/#findComment-1238392 Share on other sites More sharing options...
jcbones Posted July 6, 2011 Share Posted July 6, 2011 Even though it says registration failed, you probably still have the registered username and password in the database. Try changing the addNewUser() function to: function addNewUser($username, $password){ global $conn; $q = "INSERT INTO users VALUES ('$username', '$password')"; mysql_query($q,$conn); return mysql_affected_rows(); } Quote Link to comment https://forums.phpfreaks.com/topic/241038-cant-find-a-single-login-script-tutorial-that-works/#findComment-1238749 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.