0

I don't really know what is going wrong. I have my logout page which I redirected to the login folder:

<?php

//include $_SERVER['DOCUMENT_ROOT'].'/connect.php';
include $_SERVER['DOCUMENT_ROOT'].'/functions.php';

if(session_destroy()){
    if(isset($_COOKIE['user_name'])){
setcookie("user_name", $user_name, time()-2592000); 
    }
}
    header('location:'.$_SERVER['DOCUMENT_ROOT'].'/login/');
?>

But it always redirects me back to my index page even when I try to run the code like this:

<?php

//include $_SERVER['DOCUMENT_ROOT'].'/connect.php';
//include $_SERVER['DOCUMENT_ROOT'].'/functions.php';

session_start();

if(session_destroy()){
    if(isset($_COOKIE['user_name'])){
setcookie("user_name", $user_name, time()-2592000); 
    }
}
?>

Without including any external page and no header location it still redirects me back to the index page and secondly it doesn't destroy the session after redirecting to the index page until I reload it. Please do any one know the possible cause?

7
  • 4
    You should provide your .htaccess code here too Commented May 7, 2016 at 16:41
  • I am using WAMP local sever and even when I remove the inclusion it still does same thing. Commented May 7, 2016 at 16:44
  • Provide your .htaccess code. We cannot help you without it. Commented May 7, 2016 at 16:46
  • 1
    header('location:'.$_SERVER['DOCUMENT_ROOT'].'/login/') - that makes no sense whatsoever. Step 1: Stop confusing file system paths with URLs. Commented May 7, 2016 at 16:49
  • Bad to say, I didn't create any .htaccess code Commented May 7, 2016 at 16:52

1 Answer 1

1

$_SERVER['DOCUMENT_ROOT'] gives the directory root of the file. For inclusion or redirect $_SERVER['SERVER_NAME'] along with http:// or https:// is necessary to use.

   <?php

    $base_url = 'http://'.$_SERVER['SERVER_NAME'];


    include $base_url.'/functions.php';

    if(session_destroy()){
        if(isset($_COOKIE['user_name'])){
    setcookie("user_name", $user_name, time()-2592000); 
        }
    }
        header('location:'. $base_url .'/login/');
    ?>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.