Skip to content
View in the app

A better way to browse. Learn more.

Web Designer Forum

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

User Login - with redirect

Featured Replies

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

  • 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!

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(); :search:

 

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

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 :p

  • 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?

  • 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(); :search:

 

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?

  • Author

Oh ok well then this line can be misleading

thats what through me off :p

 

 

Once again, sorry, but still I am left in a bit of limbo as I am unsure where to add the code :(

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

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.