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. :)

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.