September 20, 201016 yr Hi Guys, Having issues with a user login that I am creating. The login works, BUT i need to redirect the page after a successful entry of information. Please please advise The script that is called is as follows: - <?php // username to login into page define('LOGIN_USER', "admin"); // password to login into page define('LOGIN_PASS', "secret"); ################################################################################################### ################################################################################################### ################################################################################################### # CLASS desc: for calling login authentication # CLASS req: looks for constants LOGIN_USER and LOGIN_PASS # Can be called: ?action=clear_login ?action=prompt class Login { // unique prefix that is used with this object (on cookies and password salt) var $prefix = "login_"; // days "remember me" cookies will remain var $cookie_duration = 21; // temporary values for comparing login are auto set here. do not set your own $user or $pass here var $user = ""; var $pass = ""; #-############################################# # desc: calls the rest of the functions depending on login state # returns: nothing, but will print login prompt and die if necessary function authorize() { //save cookie info to session if(isset($_COOKIE[$this->prefix.'user'])){ $_SESSION[$this->prefix.'user'] = $_COOKIE[$this->prefix.'user']; $_SESSION[$this->prefix.'pass'] = $_COOKIE[$this->prefix.'pass']; } // else{echo "no cookie<br>";} //if setting vars if(isset($_POST['action']) && $_POST['action'] == "set_login"){ $this->user = $_POST['user']; $this->pass = md5($this->prefix.$_POST['pass']); //hash password. salt with prefix $this->check();//dies if incorrect //if "remember me" set cookie if(isset($_POST['remember'])){ setcookie($this->prefix."user", $this->user, time()+($this->cookie_duration*86400));// (d*24h*60m*60s) setcookie($this->prefix."pass", $this->pass, time()+($this->cookie_duration*86400));// (d*24h*60m*60s) } //set session $_SESSION[$this->prefix.'user'] = $this->user; $_SESSION[$this->prefix.'pass'] = $this->pass; } //if forced log in elseif(isset($_GET['action']) && $_GET['action'] == "prompt"){ session_unset(); session_destroy(); //destroy any existing cookie by setting time in past if(!empty($_COOKIE[$this->prefix.'user'])) setcookie($this->prefix."user", "blanked", time()-(3600*25)); if(!empty($_COOKIE[$this->prefix.'pass'])) setcookie($this->prefix."pass", "blanked", time()-(3600*25)); $this->prompt(); } //if clearing the login elseif(isset($_GET['action']) && $_GET['action'] == "clear_login"){ session_unset(); session_destroy(); //destroy any existing cookie by setting time in past if(!empty($_COOKIE[$this->prefix.'user'])) setcookie($this->prefix."user", "blanked", time()-(3600*25)); if(!empty($_COOKIE[$this->prefix.'pass'])) setcookie($this->prefix."pass", "blanked", time()-(3600*25)); $msg = '<h2 class="msg">**Logout complete**</h2>'; $this->prompt($msg); } //prompt for elseif(!isset($_SESSION[$this->prefix.'pass']) || !isset($_SESSION[$this->prefix.'user'])){ $this->prompt(); } //check the pw else{ $this->user = $_SESSION[$this->prefix.'user']; $this->pass = $_SESSION[$this->prefix.'pass']; $this->check();//dies if incorrect } }#-#authorize() #-############################################# # desc: compares the user info # returns: nothing, but will print login prompt and die if incorrect function check(){ if(md5($this->prefix . LOGIN_PASS) != $this->pass || LOGIN_USER != $this->user){ //destroy any existing cookie by setting time in past if(!empty($_COOKIE[$this->prefix.'user'])) setcookie($this->prefix."user", "blanked", time()-(3600*25)); if(!empty($_COOKIE[$this->prefix.'pass'])) setcookie($this->prefix."pass", "blanked", time()-(3600*25)); session_unset(); session_destroy(); $msg='<h2 class="warn">Incorrect username or password</h2>'; $this->prompt($msg); } }#-#check() #-############################################# # desc: prompt to enter password # param: any custom message to display # returns: nothing, but exits at end function prompt($msg=''){ ?> <html><head> <title>Login</title> <style> body{margin:15px;} table.login{border-collapse:collapse;} table.login td{font:bold 10pt verdana;color:black;border:1px #535353 solid;border-collapse:collapse;padding:2px 3px;text-align:center;background:#eeeeee;} table.login td.header{background-color:#cccccc;} .msg{font:bold 120% verdana;text-align:center;color:green;} .warn{font:bold 120% verdana;text-align:center;color:maroon;} </style> </head><body> <form action="<?php echo $_SERVER['SCRIPT_NAME']; ?>" method="post"> <input type="hidden" name="action" value="set_login"> <?php echo $msg; ?> <table align="center" width="300" class="login"> <tr><td class="header" colspan="2">Enter Login Info</td></tr> <tr> <td class="desc"><label for="user">Username:</label> <input type="text" name="user" id="user"></td> <td class="desc"><label for="pass">Password:</label> <input type="password" name="pass" id="pass"></td> </tr> <tr><td class="desc" colspan="2" style="text-align:left;"> <input type="checkbox" name="remember" id="remember"> <label for="remember">Remember me on this computer</label> </td></tr> <tr><td class="desc" colspan="2" style="text-align:right;"><input type="submit" value="Login"></td></tr> </table> </form> </body></html> <?php //don't run the rest of the page exit; }#-#prompt() }//CLASS Login ?> SCRIPT USED TO GENERATE FORM ON PAGE - <?php session_start(); // start session cookies require("Login.class.php"); // pull in file $login = new Login; // create object login $login->authorize(); // make user login ?> Best, Jon
September 20, 201016 yr Author use header() to perform the redirect if the users credentials are correct. What is the actual PHP for this? Sorry to be a numptey!
September 20, 201016 yr What is the actual PHP for this? Sorry to be a numptey! header("Location:index.php"); are u sure u wrote that login script? otherwise this is a very stright forward thing that any php coder would know i mean anyone who can write a login class surly would know how to use header(); EDIT: And i'd also like to point this out u really should be using private and public variables like this private $user; private $pass; public $user; public $pass; using var $user; is not supported in php 5 always code for the newest technology not for the oldest
September 20, 201016 yr THat not entirely true. You should code to the version of PHP you currently. E.g. I'm currently working on a small news area for a website hosted by my local council that is still using PHP 4. I guess you wont be using namespaces then
September 20, 201016 yr Author THat not entirely true. You should code to the version of PHP you currently. E.g. I'm currently working on a small news area for a website hosted by my local council that is still using PHP 4. Sorry, think I may have misc-communicated something. I didn't write the script - its an open source file that I am trying to hack for my own use. Have only really just started on PHP. What I am trying to do is to start the session based on weather the user credentials are correct. Fore which I assume it will need a if function included. the issue that i am having is that my coding ability is not up to scratch in terms of PHP How is it best to alter the existing script to run the redirect?
September 20, 201016 yr Author header("Location:index.php"); are u sure u wrote that login script? otherwise this is a very stright forward thing that any php coder would know i mean anyone who can write a login class surly would know how to use header(); EDIT: And i'd also like to point this out u really should be using private and public variables like this private $user; private $pass; public $user; public $pass; using var $user; is not supported in php 5 always code for the newest technology not for the oldest Sorry, think I may have misc-communicated something. I didn't write the script - its an open source file that I am trying to hack for my own use. Have only really just started on PHP. What I am trying to do is to start the session based on weather the user credentials are correct. Fore which I assume it will need a if function included. the issue that i am having is that my coding ability is not up to scratch in terms of PHP How is it best to alter the existing script to run the redirect?
September 20, 201016 yr Oh ok well then this line can be misleading Having issues with a user login that I am creating. thats what through me off
September 20, 201016 yr Author Oh ok well then this line can be misleading thats what through me off Once again, sorry, but still I am left in a bit of limbo as I am unsure where to add the code
September 20, 201016 yr Its really difficult to work out whats going on with that code but this should do it. function check(){ if(md5($this->prefix . LOGIN_PASS) != $this->pass || LOGIN_USER != $this->user){ //destroy any existing cookie by setting time in past if(!empty($_COOKIE[$this->prefix.'user'])) setcookie($this->prefix."user", "blanked", time()-(3600*25)); if(!empty($_COOKIE[$this->prefix.'pass'])) setcookie($this->prefix."pass", "blanked", time()-(3600*25)); session_unset(); session_destroy(); $msg='<h2 class="warn">Incorrect username or password</h2>'; $this->prompt($msg); } else { header('Location: /secret.php'); } }#-#check()
Create an account or sign in to comment