Hello,

I am learning PHP. Now, I am creating login page.

This is my index.php codes

  <?php
    require "../config.php";
    session_start();

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

    if(!$username && !$password){   
        header('Location: login.php');
    } else {
        echo $hello.$username;
    }

    ?>

And this is my login.php

<?php
require "../config.php";
session_start();

function loginForm(){

        echo "<form method='post' action='?action=dologin'>"
                ."<div id='login-box'>"
                ."<span class='label'>Username</span>"
                ."<input type='text' name='username'><br>"
                ."<span class='label'>Password</span>"
                ."<input type='text' name='password'><br>"
                ."<input type='submit' value='Submit'>"
                ."</div>"
                ."</form>"
        ;

}

function login(){
    $username = mysql_real_escape_string($_POST['username']);
    $password = mysql_real_escape_string($_POST['password']);


    $query = mysql_query("SELECT * FROM user WHERE username='$username' AND password='$password'");
    $row = mysql_fetch_array($query);
    $id = $row['id'];
    $username = $row['username'];


    session_register("id", $id);
    session_register("username", $username);
    session_register("password", $password);

switch($action){
    case 'dologin';
        login();    
    break;

    default:
        loginForm();
}

?>

When user submit the loginForm, it should be go to login() but it just go back to the form. I think the problem is in the switch case. How to fix it ?

Thank you

Member Avatar for diafol

No need for this is there?

$password = $_SESSION['password'];

These are outdated:

 session_register("id", $id);
 session_register("username", $username);
 session_register("password", $password);

Use...

 $_SESSION["id"] = $id;
 $_SESSION["username"] = $username;

The loginForm function seems to just output HTML - so why not provide an include file

function loginForm(){
      include 'forms/loginform.html';
}

or ...

<?php
function loginForm(){
?>
    <form method='post' action='?action=dologin'>
        <div id='login-box'>
            <span class='label'>Username</span>
            <input type='text' name='username'><br>
            <span class='label'>Password</span>
            <input type='text' name='password'><br>
            <input type='submit' value='Submit'>
        </div>
    </form>
<?php
}
?>

Anyway...

switch($action){
    case 'dologin';
        login();    
    break;
    default:
        loginForm();
}

$action is not declared anywhere. You are depending on $_GET['action'] to set it, but I can't see it - so in that case you will always run loginForm().

Thank you for your help.

The swith case problem is fixed.

Now, I am looking for 'how to get the username from session ?'.

updated:

Sorry, it was fixed. I have got the solution.

Member Avatar for diafol

OK, mark as solved?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.