3

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.

alwaystrying
  • 163
  • 1
  • 1
  • 9

3 Answers3

1

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.

Community
  • 1
  • 1
vaso123
  • 12,347
  • 4
  • 34
  • 64
  • where about's should i add these? I include stuff i want on evey page on my include/header.php file, should i just include them on the top of that? – alwaystrying Dec 09 '14 at 13:29
  • include before you send any output. It could be everywhere on your page, just there should be no any characters, also not a space, or anything before you use the header(). please read my linked stackoverflow page. – vaso123 Dec 09 '14 at 13:36
0

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.

Pupil
  • 23,834
  • 6
  • 44
  • 66
0

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();
SaidbakR
  • 13,303
  • 20
  • 101
  • 195
  • @alwaystrying Very good to hear that! Please select it as an answer if it is the suitable solution for you. – SaidbakR Dec 09 '14 at 22:49