Jump to content

[SOLVED] Unexpected 0 on my login page


mcdof001

Recommended Posts

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

Link to comment
Share on other sites

<?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');
}
}

?>

Link to comment
Share on other sites

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;
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

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.