1

I have a logout function that looks like this.

if ($_GET["argument"]=='logOut'){
    if(session_id() == '') {
        session_start();
    }
    session_unset();
    session_destroy();
    $host  = $_SERVER['HTTP_HOST'];
    $extra = 'index.php';
    header("Location: http://$host/$extra");
    exit; 
}

My problem is that, If I inspect the page and look at the Network preview and response, it looks 'fine'.

There are two php files listed that was processed.

http://localhost:5000/inc/mainScripts.php?argument=logOut

which is where the function is located.

and http://localhost:5000/index.php

Which is where i would like to be redirected.

The Response tab in the Network of the inspect page area in chrome contains the full login html page login.php but in the browser it remains in the same place. Like the header command has never been called.

What Am I doing wrong?

HTML AJAX call to this function:

        $("#logout_btn").click(function() {
            $.ajax({
                url: './inc/mainScripts.php?argument=logOut'
            })
        });

SOLUTION

AJAX

        $("#logout_btn").click(function() {
            $.ajax({
                url: './inc/mainScripts.php?argument=logOut',
                success: function(data){
                    window.location.href = data;
                }
            });
        });

PHP

if ($_GET["argument"]=='logOut'){
    if(session_id() == '') {
        session_start();
    }
    session_unset();
    session_destroy();
    $host  = $_SERVER['HTTP_HOST'];
    $link = "http://$host/index.php";
    echo $link; 
}
6
  • what is $extra when you use it? Commented Aug 12, 2014 at 9:17
  • What do you mean. its 'index.php' Commented Aug 12, 2014 at 9:23
  • ok so I am a little confused, are you saying the browser is reporting it as index.php when you expected it to show login.php or the browser is still reporting it as mainScripts.php?argument=logOut when it should be index.php? Commented Aug 12, 2014 at 9:26
  • Browser shows http://localhost:5000/Main.php main content page. but inspecting the page the network tab shows php calling the function and then showing index.php but nothing changes on the browser. I will append a image Commented Aug 12, 2014 at 9:29
  • 3
    Show the HTML of the logout link/button. It sounds like you're doing the logout as an AJAX call. Commented Aug 12, 2014 at 9:30

2 Answers 2

2

Try this instead.

if( isset($_GET['argument']) && $_GET['argument'] == 'logOut' && !empty( session_id() ) ) {

    session_destroy();
    header("Location: http://" . $_SERVER['HTTP_HOST'] . "/index.php");
    exit;

}

Edit: If you're using AJAX, it'd be easier to send the url from your php script back to your javascript and redirect from there.

Sign up to request clarification or add additional context in comments.

1 Comment

Yes, the EDIT section redirect with jquery worked. thank you
2

You are probably running into the same common problem that many people run into when people first start to program in PHP.

Calls to header() only works when there are NO previous HTML output generated. If there are any HTML output generated, even just a single space, calls to header() will fail. To get around this problem, use functions such as ob_start() and ob_end_flush().

Comments

Your Answer

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