-1

I've got a weird problem. I created a application in PHP JS etc. And everything was fine. All headers were working fine too. But today I just want to check my website and it just stopped working.

Just like this code:

<body>
<?php
session_start();
session_destroy();
header("Location: index.php?error=logout");
exit;
?>
</body> 

I don't see there any output, any echos. But it still doesn't work.

Can someone tell me what's wrong? Maybe something has changed in PHP specification without my mind?

I just edited it to:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Voter</title>

<link rel="Stylesheet" type="text/css" href="style.css" />

</head>

<?php
session_start();
session_destroy();
header("Location: index.php?error=logout");
?>
</html>

That's the full code of the website.

When I just deleted all other code, like this:

<?php
session_start();
session_destroy();
header("Location: index.php?error=logout");
?>

It works fine. But why it was working before, and now i had to change it?

Sebastian B
  • 55
  • 1
  • 8
  • 1
    You have an "Headers already sent" error. You may not echo output (here: body) in front of re-location (or other headers like e.g. cookies) – djot Mar 06 '14 at 20:14
  • 3
    Why do you enclose it in a `` if it won't shown? – zerkms Mar 06 '14 at 20:14
  • take out and and make sure there is no newline or white space after ?> – Abhik Chakraborty Mar 06 '14 at 20:14
  • 1
    @AbhikChakraborty: It's the whitespace *before* the `` doesn't matter. – gen_Eric Mar 06 '14 at 20:16
  • see answer given here for more information: http://stackoverflow.com/questions/8028957/how-to-fix-headers-already-sent-error-in-php – djot Mar 06 '14 at 20:16
  • I didn't delete because i use it in other sites and it was working. Even this code was working too. But now it just stopped working. – Sebastian B Mar 06 '14 at 20:18
  • 1
    @user3389896: when you program you should follow the common sense, not "it worked somewhere else". You are not going to show it. **WHY** do you put it there then? What is the technical reason for that? A note: "because I can" is not a technical reason – zerkms Mar 06 '14 at 20:20
  • @user3389896: Just as a test, does it work *if* you delete ``? Having it there is *incorrect*. – gen_Eric Mar 06 '14 at 20:22
  • Oh thats great! evewn without it don't work ;/ there is not any other code ;o – Sebastian B Mar 06 '14 at 20:25
  • @user3389896: it does work – zerkms Mar 06 '14 at 20:28
  • Maybe it's a problem with php.ini or smthing? I didn't change anything in other code ;o – Sebastian B Mar 06 '14 at 20:32
  • @user3389896: explain what "doesn't work" means and update the question with the actual code that "doesn't work" – zerkms Mar 06 '14 at 20:34
  • 2
    @user3389896: OH MY GOD. **WHY** do you put doctype and other things? Are you going to render this page? PS: if you haven't ignored my question about "technical reason" for doing that - you would know that what you're doing makes no sense (but for some reason you prefer to do random stuff and expect it to work - good luck with that) – zerkms Mar 06 '14 at 20:40
  • Your edited code won't work for the reason detailed by @djot `header()` must be called before outputting ANYTHING. More to the point, that code didn't "just stop working", it never could have worked. – The Blue Dog Mar 06 '14 at 20:43
  • So how i have to redirect my website after this things? I don't need that in this code. That's right. But in other sites, i have doctypes, and it worked! Really. So how do u explain that it was working in other sited, in this even too. – Sebastian B Mar 06 '14 at 20:53
  • @user3389896: Your PHP might have been configured in an odd way. You also probably have error reporting disabled (you should turn that on in a development environment). Are you using any output buffering (`ob_start()` and friends)? – gen_Eric Mar 06 '14 at 21:03
  • No I'm not. I don't fully understand ob_start(). I used it but it won't work too. Maybe i used it in wrong way. – Sebastian B Mar 06 '14 at 21:05

2 Answers2

1

Try this:

<?php
session_start();
session_destroy();
header("Location: /index.php?error=logout");
?>
jx12345
  • 1,650
  • 2
  • 22
  • 40
  • I understand that You want to delete but the question is why? It should not show an error. Isn't it? – Sebastian B Mar 06 '14 at 20:19
  • You can't output anything before a header() call. see this: http://uk1.php.net/manual/en/function.header.php – jx12345 Mar 06 '14 at 20:20
  • @user3389896: "It should not show an error" --- it should – zerkms Mar 06 '14 at 20:20
  • @user3389896: You probably have error reporting turned off. It *is* showing an error, you're probably just hiding it. – gen_Eric Mar 06 '14 at 20:21
  • Ok i just used that what you told me. and... It still dont work ;o why? – Sebastian B Mar 06 '14 at 20:25
  • could you try the full path... ie. header("Location: http://www.your-domain.com/index.php?error=logout"); Also make sure that the page your redirecting to eg. http://www.your-domain.com/index.php?error=logout is working fine on it own. – jx12345 Mar 06 '14 at 20:28
  • I changed that for full address but it still doesn't work ;/ Is this possible that it changed itself? I didn't changed this code since 2 month's. It was working, day by day. But today just stopped. Without any code change ;o – Sebastian B Mar 06 '14 at 20:32
  • 1
    @user3389896: so answer stated you to only put those 5 lines. Whereas you put additional doctype and other stuff. What idea did you follow when you did that? Do you understand how this code works? Do you understand what doctype is for? – zerkms Mar 06 '14 at 20:42
  • OK I understand that I am stupid. I read about header() and know that i have to be clear before use it. But now i can't understand that, how it work before? Can someone explain me that? ;/ I was using header() like that in about 6 pages. All were working fine. But now it just stopped and i was wondering why. That's why I am arguing with You. I just want to understand that. – Sebastian B Mar 06 '14 at 21:02
0

Header only works if it no other text has been printed.

So, the header command must come before the

<head>

So for example,

<?php
session_start();
session_destroy();
header("Location: index.php?error=logout");
exit;
?>
<body>
</body> 
Cameron
  • 572
  • 1
  • 3
  • 12
  • Only before ``? Final answer? Phone a friend? – The Blue Dog Mar 06 '14 at 20:56
  • I changed it. Updated at the main thread. Now it work's. But how i can redirect my websites after some outputs? I need that ;/ – Sebastian B Mar 06 '14 at 21:00
  • If you want to redirect your webpage after the content has loaded you will have to do this in Javascript. Maybe add a javascript to the HTML document, which will reload the page after 10 or 15 seconds. – Cameron Mar 06 '14 at 21:48
  • Yeah you can do that in javascript have a look at this thread: http://stackoverflow.com/questions/503093/how-can-i-make-a-redirect-page-in-jquery-javascript – jx12345 Mar 07 '14 at 09:18