Can you help me with a simple javascript, PHP, and cookie login script? - I Have all...

Christopher

New member
...the code, whats wrong? Ok first I have a normal .HTML page that has the following form on it.

<form method="post" action="login.php">
Username:<input type="text" name="user" id="user">
Password:<input type="password" name="pass" id="pass">
<input type="submit" value="Login"> <input type="reset" value="Clear"> </form>

This form gets the person to enter their username and password and sends this data to LOGIN.PHP, this is just a separate page that has a predetermined password and username set up (It would be nice to add more than one, but for now just one will be fine)

This is the PHP script...when the correct username and password are entered it does work and send them to the right page BUT IT DOES NOT set a cookie which is what I'll need for the next step.

<?php
if (isset($_POST["user"]) && isset($_POST["pass"])
&& strtolower($_POST["user"]) == "admin" && $_POST["pass"] == "password") {
setcookie( "userlogin" , "admin" , time()+60*60*24*0 , "/" , "www.mysitehere.com" );
header( "Location: http://www.mysitehere.com/pages/page1.html" );
} else {
header( "Location: http://www.mysitehere.com/pages/login_fail.html" );
}
?>

NOW CAN YOU help me fix the two parts I'm missing?

1) A proper cookie that will last for one day, delete itself on browser close, and be deleted if the person hits a logout button that I don't have (Can you show me how?) yet.

2) A Java Script that I can place in the HEAD of my protected pages that checks if that cookie exist. If it doesn't they can not be on that page and get redirected to a login page or whatever page I choose to send them too.

THANKS FOR ALL THOSE WHO HELP!!! - This is simple I know it, just not something i know how to do yet. 5 stars to whoever can help fix my code and get it all working!
I'm not protecting anything important and I do not want my pages to be PHP. This is why I need to check with javascript.

EXAMPLE > login.html (sends data to loginscript.php) Pass and user correct so page1.html loads. When i click on a link and change to page2.html a Java Script in the head checks to see if the login cookie exists and allows you to stay if it does. IF it doesnt it redirects you to login.html
 
Back
Top