Jump to content

Trying to create a login script from scratch.


Noskiw

Recommended Posts

I've decided to "re-learn" PHP.

 

I've created a registration script which works well, however, the login script is not going to plan for me.

 

This is the script:

 

<?php include "./global/global.php"; ?>
<?php error_reporting (E_ALL ^ E_NOTICE); ?>

<?php

$username = $_POST['username'];
$password = $_POST['password'];

$submit = $_POST['submit'];

if($submit){
    $sql1 = "SELECT * FROM test2 WHERE username = '".$username."'";
    $res1 = mysql_query($sql1) or die(mysql_error());
    
    $numrows = mysql_num_rows($res1);
        
    if($numrows != 0){
        while($row = mysql_fetch_assoc($res1)){
            $dbusername = $row['username'];
            $dbpassword = $row['password'];
        }
        
        if($username==$dbusername && $password==$dbpassword){
            ob_start();
            $_SESSION['username']=$username;
            header("Location: index.php");
        }
    }else{
        echo("That user doesn't exist!");
    }               
}else{
    
    ?>
    <form action="login.php" method="POST">

    <input type="text" name="username" />
    <input type="password" name="password" />
    <input type="submit" name="submit" value="Login" />

    </form>
    
<?php

}

?>

 

The problem is that it isn't building the session, so when I go back to the homepage, which looks like this:

 

<?php include "./global/global.php"; session_start(); ?>
<?php error_reporting (E_ALL ^ E_NOTICE); ?>

<html>

<?php

if($_SESSION){
    echo("Welcome, ".$_SESSION['username']);
}else{
    echo("Please login <a href='login.php'>here</a>");
}

?>

</html>

 

It shows that I should login.

 

I can't seem to see what's wrong because I have a script very similar to it and it works fine, so if anyone could help, I'd appreciate it. :)

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.