I've made a log in use OO in php. Everything is working fine with the sessions and everything else. The only problem I'm having is my redirect does not work after the user has logged in and after the user has registered. I'm pretty sure it's because I'm using the header tag, but I'm not sure how else to do this. I tried using jscript to redirect but didn't have any luck with that either.
Here's my log in code (this should redirect the user to the 'index.php' page):
<title>D2W Embroidery & Print</title>
<?php require 'includes/header.php'; ?>
<?php include 'includes/nav.php'; ?>
<div id="content" class="two-thrids columns">
<h3>Log In</h3>
<?php
if(session::exists('home')) {
echo '<p>', session::flash('home'), '</p>'; //displays message after users has register which is removed after page refresh
}
if(input::exists()) {
if(token::check(input::get('token'))) {
$user = new user();
$remember = (input::get('remember') === 'on') ? true : false; //detects if users has ticked the remember me box
$login = $user->login(input::get('username'), input::get('password'), $remember);
if($login) {
redirect::to('index.php');
} else {
echo '<p>Sorry, that username and password wasn\'t recognised.</p>';
}
}
}
?>
<form action="" method="post">
<div class="field">
<label for="username">Username:</label>
<input type="text" name="username" id="username">
</div>
<div class="field">
<label for="password">Password:</label>
<input type="password" name="password" id="password">
</div>
<div class="field">
<label for="remember">
<input type="checkbox" name="remember" id="remember">Remember me
</label>
</div>
<input type="submit" value="Log in">
<input type="hidden" name="token" value="<?php echo token::generate(); ?>">
</form>
and this is the code for the redirect:
<?php
class redirect {
public static function to($location = null) {
if($location) {
if(is_numeric($location)) {
switch($location) {
case 404:
header('HTTP/1.0 404 Not Found');
include 'includes/errors/404.php';
exit();
break;
}
} else {
header('Location: ' . $location);
die();
}
}
}
}
?>
this is the javascript redirect I tried. I've never used this before so not sure if I'm doing it right
echo '<script type="text/javascript">
window.location = '. $location .'
</script>';
die();
Any help would be great.