1

I want use a single php file to handle all of my voting requests.

Currently the script will, if siteType isn't set, forward the user and display a message. If the user has JS on then it will return a json object and ajax the page.

        if(!isset($_COOKIE['siteType'])){
            displayMessages('bad', 'Before you can continue you must select which category you would like to be in.');
            header('Location:/');
            exit;
        }

I need it as that if this php code above is executed the page will reload, i assume with javascript and reading the http headers?

[Edit] I didn't make myself clear enough, but this is a ajax request. I don't output any html. So this really is just about getting js to handle the header?

2
  • 2
    Your not allowed to use header() AFTER outputting data to the HTTP stream. Commented Jul 1, 2012 at 17:30
  • so... JavaScript will do this already? Commented Jul 1, 2012 at 17:41

3 Answers 3

3

You can't Refreshing a page with javascript using php header('location') Because, header('Location: xxx'); must be the only output of your PHP script, it is a header, you can not put it after javascript syntax

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

2 Comments

but i want my js to handle the header... why would but the header before the javascript syntax? Maybe i didn't make myself clear
while this wasn't a solution to my problem it still gives good information on the issue for others.
1

PHP

echo '<meta http-equiv="refresh" content="0">';

Javascript

window.location.reload();

4 Comments

its a ajax json request. I won't this to work as i can't output any html
cheer fella. Boy i don't think i made the question clear enough. So the javascript won't know when to reload the page.
The javascript will know when to reload the page, if you put that javascript code inside braces { }.
I am sure. if(!isset($_COOKIE['siteType'])){ window.location.reload(); }
0

Maybe this would be some solution for you,

if(!isset($_COOKIE['siteType'])){
     displayMessages('bad', 'Before you can continue you must select which category you would like to be in.');
     echo '<script>window.location.reload()</script>';
     exit;
 }

1 Comment

I'd go with a meta refresh tag instead of a script in case JS is disabled.

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.