mcdof001 Posted August 19, 2007 Share Posted August 19, 2007 Hi, sorry I am not at all good at PHP and are just starting out, anyway I'm trying to intergrate a Login PHP System and it was all going well till I noticed a 0 come up at the bottom of my page. On the original script (before I edited it to fit in with my website) it never came up but now I don't know how to get rid of it. It appears in a little table that shows the result of the login, ie. the errors. But now before the user logs in/when they go onto the page a zero pops up. I wanted to keep the system fairly simple and get rid of all the css stuff. Here is the edited source code that gives me the 0. <?php require_once('common.php'); $error = '0'; if (isset($_POST['submitBtn'])){ // Get user input $username = isset($_POST['username']) ? $_POST['username'] : ''; $password = isset($_POST['password']) ? $_POST['password'] : ''; // Try to login the user $error = loginUser($username,$password); } ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "DTD/xhtml1-transitional.dtd"> <html> <head> <title>CRAP TV</title> <style type="text/css"> <!-- .style1 {color: #FFFFFF} body { background-color: #000000; } .style2 {color: #000000} --> </style> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /></head> <body link="#FFFFFF"> <?php if ($error != '') {?> <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" name="loginform"> <table width="300" align="center"> <tr><td><span class="style1">Username:</span></td> <td> <input class="text" name="username" type="text" /></td></tr> <tr><td><span class="style1">Password:</span></td> <td> <input class="text" name="password" type="password" /></td></tr> <tr><td colspan="2" align="center"><input class="text" type="submit" name="submitBtn" value="Login" /></td></tr> <div align="center"><a href="register.php">Register</a> </div> </table> </form> <?php } ?> <?php if (isset($_POST['submitBtn'])){ ?> <?php } ?> <div id="result"> <table width="100%" align="center"> <tr><td class="style1"><div align="center"><span class="style2"><br/> <?php if ($error == '') { echo "Welcome $username! <br/>You are logged in!<br/><br/>"; echo '<a href="main.php">Click here to enter!</a>'; } else echo $error; ?> </span><br/> <br/> <br/> </div></td></tr></table> </body> </html> Here is the scripts original source code:- <?php require_once('common.php'); $error = '0'; if (isset($_POST['submitBtn'])){ // Get user input $username = isset($_POST['username']) ? $_POST['username'] : ''; $password = isset($_POST['password']) ? $_POST['password'] : ''; // Try to login the user $error = loginUser($username,$password); } ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "DTD/xhtml1-transitional.dtd"> <html> <head> <title>CRAP TV</title> <link href="style/style.css" rel="stylesheet" type="text/css" /> </head> <body> <div id="main"> <?php if ($error != '') {?> <div class="caption">Site login</div> <div id="icon"> </div> <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" name="loginform"> <table width="100%"> <tr><td>Username:</td><td> <input class="text" name="username" type="text" /></td></tr> <tr><td>Password:</td><td> <input class="text" name="password" type="password" /></td></tr> <tr><td colspan="2" align="center"><input class="text" type="submit" name="submitBtn" value="Login" /></td></tr> </table> </form> <a href="register.php">Register</a> <?php } if (isset($_POST['submitBtn'])){ ?> <div class="caption">Login result:</div> <div id="icon2"> </div> <div id="result"> <table width="100%"><tr><td><br/> <?php if ($error == '') { echo "Welcome $username! <br/>You are logged in!<br/><br/>"; echo '<a href="index.php">Click here to enter!</a>'; } else echo $error; ?> <br/><br/><br/></td></tr></table> </div> <?php } ?> <div id="source">CRAP TV</div> </div> </body> Thanks In Advance, mcdof001 Quote Link to comment https://forums.phpfreaks.com/topic/65681-solved-unexpected-0-on-my-login-page/ Share on other sites More sharing options...
MadTechie Posted August 19, 2007 Share Posted August 19, 2007 i think the problem is in the loginUser function also try changing $error = '0'; to $error = ''; but as i said i think thats not really the problem Quote Link to comment https://forums.phpfreaks.com/topic/65681-solved-unexpected-0-on-my-login-page/#findComment-328020 Share on other sites More sharing options...
mcdof001 Posted August 19, 2007 Author Share Posted August 19, 2007 Thanks for your reply, but no changing "0" to "" didn't work and just messed up the script. I know what you are saying but like you I just can't put my finger on it. Quote Link to comment https://forums.phpfreaks.com/topic/65681-solved-unexpected-0-on-my-login-page/#findComment-328039 Share on other sites More sharing options...
plutomed Posted August 19, 2007 Share Posted August 19, 2007 Btw have you not used sessions? Quote Link to comment https://forums.phpfreaks.com/topic/65681-solved-unexpected-0-on-my-login-page/#findComment-328050 Share on other sites More sharing options...
MadTechie Posted August 19, 2007 Share Posted August 19, 2007 i think the problem is in the loginUser function can you post that code then!! Quote Link to comment https://forums.phpfreaks.com/topic/65681-solved-unexpected-0-on-my-login-page/#findComment-328062 Share on other sites More sharing options...
mcdof001 Posted August 19, 2007 Author Share Posted August 19, 2007 <?php session_start(); function registerUser($user,$pass1,$pass2){ $errorText = ''; // Check passwords if ($pass1 != $pass2) $errorText = "Passwords are not identical!"; elseif (strlen($pass1) < 6) $errorText = "Password is too short!"; // Check user existance $pfile = fopen("userpwd.txt","a+"); rewind($pfile); while (!feof($pfile)) { $line = fgets($pfile); $tmp = explode(':', $line); if ($tmp[0] == $user) { $errorText = "The selected user name is taken!"; break; } } // If everything is OK -> store user data if ($errorText == ''){ // Secure password string $userpass = md5($pass1); fwrite($pfile, "\r\n$user:$userpass"); } fclose($pfile); return $errorText; } function loginUser($user,$pass){ $errorText = ''; $validUser = false; // Check user existance $pfile = fopen("userpwd.txt","r"); rewind($pfile); while (!feof($pfile)) { $line = fgets($pfile); $tmp = explode(':', $line); if ($tmp[0] == $user) { // User exists, check password if (trim($tmp[1]) == trim(md5($pass))){ $validUser= true; $_SESSION['userName'] = $user; } break; } } fclose($pfile); if ($validUser != true) $errorText = "Invalid username or password!"; if ($validUser == true) $_SESSION['validUser'] = true; else $_SESSION['validUser'] = false; return $errorText; } function logoutUser(){ unset($_SESSION['validUser']); unset($_SESSION['userName']); } function checkUser(){ if ((!isset($_SESSION['validUser'])) || ($_SESSION['validUser'] != true)){ header('Location: login.php'); } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/65681-solved-unexpected-0-on-my-login-page/#findComment-328153 Share on other sites More sharing options...
MadTechie Posted August 19, 2007 Share Posted August 19, 2007 OK here the problem, from the first post change <?php if ($error == '') { echo "Welcome $username! <br/>You are logged in!<br/><br/>"; echo '<a href="index.php">Click here to enter!</a>'; } else echo $error; ?> to <?php if ($error == '' && isset($_POST['submitBtn'])) { echo "Welcome $username! <br/>You are logged in!<br/><br/>"; echo '<a href="main.php">Click here to enter!</a>'; }else echo $error; ?> Quote Link to comment https://forums.phpfreaks.com/topic/65681-solved-unexpected-0-on-my-login-page/#findComment-328156 Share on other sites More sharing options...
mcdof001 Posted August 19, 2007 Author Share Posted August 19, 2007 No, sorry it still seems not to work.... Quote Link to comment https://forums.phpfreaks.com/topic/65681-solved-unexpected-0-on-my-login-page/#findComment-328199 Share on other sites More sharing options...
MadTechie Posted August 19, 2007 Share Posted August 19, 2007 OK last one.. well should be! <?php if(isset($_POST['submitBtn'])) { if ($error == '') { echo "Welcome $username! <br/>You are logged in!<br/><br/>"; echo '<a href="main.php">Click here to enter!</a>'; }else { echo $error; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/65681-solved-unexpected-0-on-my-login-page/#findComment-328201 Share on other sites More sharing options...
mcdof001 Posted August 19, 2007 Author Share Posted August 19, 2007 :) :) :) :) :) Thank You So Much, Working Perfectly again now, I am sooooooooooo grateful. By The Way, PHP Freaks RULES. Quote Link to comment https://forums.phpfreaks.com/topic/65681-solved-unexpected-0-on-my-login-page/#findComment-328203 Share on other sites More sharing options...
MadTechie Posted August 19, 2007 Share Posted August 19, 2007 sorry for the many attempts (i deal with many posts at once) can you please click topic solved (bottom left) Quote Link to comment https://forums.phpfreaks.com/topic/65681-solved-unexpected-0-on-my-login-page/#findComment-328205 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.