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