Hi everyone!
I am new to PHP and aat the moment trying to create a login to an admin area of a website with a simple CMS backend. I have found this code whicj I have tried to look at/learn from and implement into my own website, but I am missing something to make it work. It doesnt even redirect to my admin area, the page with the form doesnt do anything after submitting the user and pass..It doesnt redirect to the header with location:index.php..
Help :-)
Instead I get this error in the login.php (Just above the form)
"Notice: Undefined index: act in C:\wamp\www\mycms\administrator\login.php on line 3"
Here is the code, I hope someone out there can see what the problem is, again I dont have so much experience, so perhaps I have missed something essential/simple here.. :-)
Here is the Login.php:
<?php
$act = $_GET['act']; //retrives the page action
if(empty($act)) //if there is no action
{
echo('<form action="index.php?act=auth" method="post" name="loginform" id="loginform">
<p>Username
<input type="text" name="user">
</p>
<p>Password
<input type="password" name="pass">
</p>
<p>
<input type="submit" name="Submit" value="Login">
</p>
</form>');
}
elseif($act == "auth") //if our page action = auth
{
$user = $_POST['user']; //pulls the username from the form
$pw = $_POST['pass']; //pulls the pass from the form
$pass = md5($pw); //makes our password an md5
include("connect_to_mysql.php"); //connects to our mysql database
$login = mysql_query("SELECT * FROM `users` WHERE `user` = '$user' AND `pass` = '$pass`"); //selects info from our table if the row has the same user and pass that our form does
if(!mysql_num_rows($login)) //if the username and pass are wrong
{
header("Location: login.php"); //redirects to our login page
die(); //stops the page from going any further
}
else
{
setcookie("user", $user, time()+3600);//sets our user cookie
setcookie("pass", $pass, time()+3600);//sets our pass cookie
header("Location: index.php");//instead of yourpage.php it would be your protected //page
}
}
?>
And here is the protect.php
<?php
$user = $_COOKIE['user']; //gets the user from the cookies
$pass = $_COOKIE['pass']; //gets the pass from cookies
include("connect_to_mysql.php"); // connects to our database
$login = mysql_query("SELECT * FROM `users` WHERE `user` = '$user' AND `pass` = '$pass`"); //selects info from our table if the row has the same user and pass that our cookies do
if(!mysql_num_rows($login)) //if the username and pass are wrong
{
header("Location: login.php"); //redirects to our login page
die(); //stops the page from going any further
}
?>