0

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

Edited by cakka

3
Contributors
4
Replies
29
Views
3 Years
Discussion Span
Last Post by Ejim Kingsley
1

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

0

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.

Edited by cakka

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.