I have absolutely no back around in PHP, so pardon me if I sound stupid.
I am trying to implement a login fucntion but i couldnt get the php part to work. is there any where wrong with write the code before the html tag? please kindly advise.
<?php
require "config.php";
$LS->init();
if(isset($_POST['act_login'])){
$user=$_POST['login'];
$pass=$_POST['pass'];
if($user=="" || $pass==""){
$msg=array("Error", "Username / Password Wrong !");
}else{
if(!$LS->login($user, $pass)){
$msg=array("Error", "Username / Password Wrong !");
}
}
}
?>
<html>
<head>
<title>Log In</title>
</head>
<body>
normal html code.
</body>
</html>
Ok, user2760642, I have fixed your code a little for what you had missing :
<?php
include_once "SOMECLASS.PHP";
$LS = new SOME_CLASS(); // CONTAINS METHOD TO CHECK LOGIN-PASS.
if(IsSet($_POST['login'])){ // CHECK IF USER CLICKED ENTER BUTTON.
$user=$_POST['login'];
$pass=$_POST['pass'];
if($user=="" || $pass==""){
echo "Error : Username / Password Wrong !";
}else{
if(!$LS->login($user, $pass)){
echo "Error : Username / Password Wrong !";
}else{
header("Location:mainpage.php"); // OPENS THE MAIN PAGE BECAUSE
} // LOGIN-PASS WERE ALL RIGHT.
}
}
?>
<html>
<head>
<title>Log In</title>
</head>
<body>
<form method="post" action="loginpage.php">
Login <input type="text" name="login" />
<br/>
Password <input type="text" name="pass" />
<br/>
<input type="submit" value="Click here to ENTER" />
</form>
</body>
</html>
It is wrong to add the PHP code in the same HTML file because if user presses BACK button the previous page would reload and the PHP code would be re-executed (it wouldn't happen with a separated php script file). You were missing the form asking the login and password. Finally, I added the class to check the login and password in the database (SOMECLASS.PHP).
You can copy and paste this code to a textfile named loginpage.php, and save it to the www subdirectory inside you wamp (or lamp or mamp) directory. Then just run it from address bar of your browser with localhost/loginpage.php. If it doesn't run, try localhost:80/loginpage.php (if it stills not running, call Houston because we have a problem).
I hope it helps you learning PHP.