Reputation: 163
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.
Upvotes: 3
Views: 75
Reputation: 13544
Your javascript is wrote wrong. It should be a double quote for the location ..."'.$location.'"...
echo '<script type="text/javascript">
window.location = "'. $location .'";
</script>';
die();
Upvotes: 0
Reputation: 23958
Just add ob_start() at the top of your file.
Explanation:
Your redirection is not working because, you have HTML printing on the page before redirection.
Redirection does not occur if you have some output on page.
If we use ob_start()
, all the printed output gets stored in a buffer and it can be retrieved later.
And the redirection occurs.
Upvotes: 0
Reputation: 12391
You have to use header("location: ...')
before any output. See here
Also, add error_reporting(E_ALL);
and ini_set('display_errors', 1);
while developing to catch errors like this.
So move your whole if(input::exists()) {
condition and all of it content to the really top of the file.
Upvotes: 1