0

using this code I've countered a bit of a problem. When I try to redirect to subfolder it ignores registered user and redirects me back to index.php site.

// auth.php
class myAuth {
    static function checkAuth() {
        // detect user by set cookie
        // and value which we saved in session
        if(!session_id()) session_start();
        // check ...
        if(
            isset($_COOKIE["auth"])
            &&
            isset($_SESSION["auth"])
            &&
            $_COOKIE["auth"] == $_SESSION["auth"]
        ) {
            // extend the session and cookie and in mysql as well
            self::_setCookieSessionDBTokenValidity();
            return true;
        } else {
            return false;
        }
    } // chechAuth finish

    // this funkcion redirect user
    // on main site (index.php)
    // na početnu stranicu (index.php)
    // use checkAuthWithRedirect if he's not logged in
    static function checkAuthWithRedirect() {
        if(!self::checkAuth()) {
            header('Location:index.php');
        }
    } // checkAuthWithRedirect finish
static function doLogin() {
    // register user
    // save data in session
    if(
        !empty($_POST['user'])
        &&
        !empty($_POST['pass'])
    ) {
        if(!session_id()) session_start();

        // chech and fetch data for user with sended pass
        $user = self::_fetchUserWithPassDB();


        // if we find user finish login
        if($user) {
            // strengthen pass a bit

            $token = md5(rand(100000,999999));

            // save token in session
            $_SESSION["auth"] = $token;

            // save user in session
            $_SESSION["user"] = $user[0]["user"];

            // save role in session
            $_SESSION["role"] = $user[0]["role"];

            // postavi validity i token u cookie, session i bazu
            // save validity and token in cookie, session in db
            self::_setCookieSessionDBTokenValidity();                   

            // redirect 
            header("Location:admin.php");

        }
        else {

            echo '<div class="alert alert-danger" role="alert">';
            p('<span class="glyphicon glyphicon-exclamation-sign"></span>      USER DOES NOT EXIST!');
            echo '</div>';
        }


    } 

} // login

This block of code works fine when redirect me to site which is in root folder but when I try to redirect to subfolder it completily ignores code and redirects me to index.php.

Example:

            // redirect 
            header("Location:test/admin.php");

And here is example of admin.php site

<?php
// login.php
require_once(__DIR__.'/init.php');
showHTMLHeaderWithTitle('Prijava');
myAuth::checkAuthWithRedirect();
?>


<h1>TEST TEST TEST</h1>



<?php
showHTMLFooter();
?>
3
  • you sure you're not getting a 404 here? if test isn't a sub-folder of the script's execution location, then go back to the root header("Location:/test/admin.php"); exit; or a full http call header("Location: http://www.example.com/test/admin.php"); exit; - check for errors also. May 29, 2016 at 17:28
  • you also tagged as .htaccess so we don't know if you've a bad rewrite. Again, check for errors or look at your logs, should there be something in there. I for one will not be able to pop in a magic answer; good luck. I will pass on this one and move on. May 29, 2016 at 17:36
  • .htaccess was my tag fail. Never mind, I posted question while searching for answer myself. Thank you for you time. I ll look for errors
    – Svinjica
    May 29, 2016 at 17:39

1 Answer 1

0

Replace header functions parameter - "static function checkAuthWithRedirect()" in auth.php with the following

static function checkAuthWithRedirect() {
        if(!self::checkAuth()) {
            header('Location:test/admin.php');
        }
    }

It is because, when you place redirect to test/admin.php in doLogin() within auth.php,

the myAuth::checkAuthWithRedirect(); in admin.php initiates checkAuthWithRedirect() in auth.php where it is again redirecting to index.php

Just replace the redirect value in checkAuthWithRedirect() to test/admin.php & redirect value in doLogin() to test/admin.php. It would work!

1
  • I tried to do that before and it doesn't work it creates following URL which is incorrect : test/test/admin.php and I need to redirect me to this URL test/admin.php. To be fair function checkAuth() is used to create verification on site. If the user is not logged in redirect him to index.php But I do appreciate your effort, thank you kind sir!
    – Svinjica
    May 29, 2016 at 19:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.