Header why is it not working????

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
hybris
Forum Contributor
Posts: 172
Joined: Wed Sep 25, 2013 4:09 am

Header why is it not working????

Post by hybris »

Hi I cant get the following code to function properly. It gets stuck on the page and doesn't redirect.

I know I shouldn't echo before calling header but I do not.

I know the function is working since if I replace header with echo "ok" / "not" ok it works (IE echo ok if it is supposed to and echo not ok if it is supposed to.

I can get one step to work if i put the header('Location: main.php') on top of the page and only call exit within the if statement but then I have no way to redirect to reject.php.


Code: Select all

<?php
include_once 'connect_db.php';
include_once 'incs.php';
sec_session_start(); 
if (isset($_POST $_POST['pw'],['userID'])) {
$pass = $_POST['pw'];     
$id = $_POST['id'];
    
    if (login($pass, $id, $mysqli) == ok) {
       header("Location: main.php");
       exit;
    } else {
        header("Location: reject.php");
        exit;
    }
} 
 ?>

I also tried to make 2 separate pages a and b.php with only the code <?php header(...); exit; ?> and use include a/b within the if statement but no luck..

Am I really that stupid or is there something wrong with the header command... oh and I tried with " ' and combinations within the header().. I know the header path is ok though bacause it works if i declare the header on top of the page and only call exit within the if block...

So what am I doing wrong?
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Header why is it not working????

Post by Christopher »

The only reason it would not get to one of the header() statements is if (isset($_POST $_POST['pw'],['userID'])) evaluates to false. Is that isset() missing a comma?
(#10850)
hybris
Forum Contributor
Posts: 172
Joined: Wed Sep 25, 2013 4:09 am

Re: Header why is it not working????

Post by hybris »

Sorry the code is:

if (isset( $_POST['pw'],$_POST['userID'])) {

I removed some param to errorcheck and screwed up cut n paste... the function is working.. if I substitute header vs echo "logged in"; or .."not logged in" it shows correctly to screen... my registred user shows the echoed txt logged in on screen and a fake shows not logged in..

I dont do any echo before the header.. I tried object() object_flush() but nothing is working it simply refuse to redirect me :(
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Header why is it not working????

Post by Christopher »

Have you tried using the full URL in the Location: header?
(#10850)
hybris
Forum Contributor
Posts: 172
Joined: Wed Sep 25, 2013 4:09 am

Re: Header why is it not working????

Post by hybris »

Hi yes I have.

I don't know if im stupid but I seriously cannot figure out why it is not working. The code i pasted is the only code i have in that php file... I don't do any echo before i call header...i removed all blanks..i even removed all my comments but still it is not working. Could it be something thats messed up in my browser cache or something that causes this behaviour? (I use chrome)..
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Header why is it not working????

Post by Celauran »

Trying it in incognito mode will bypass any caching issues that may exist. Have you checked your server's error logs?
hybris
Forum Contributor
Posts: 172
Joined: Wed Sep 25, 2013 4:09 am

Re: Header why is it not working????

Post by hybris »

Also i found this "fix":

Code: Select all

<script type="text/javascript">
<!--
window.location = "http://www.mysite.com/page.php"
//-->
</script>
which do the redirect for me but the securesession() function EDIT on the target page /EDIT does not regognize Im logged in so Im not sure if I got something wrong in that function or if the javascript redirect mess up the session somehow...

I think its my securesession() function that is the cause because on a similar page i have if i login i can open a new tab and write the url for my secure page and is logged in until i close the browser so I dont think the javascript would break the session right?
Last edited by hybris on Tue Mar 04, 2014 4:12 pm, edited 1 time in total.
hybris
Forum Contributor
Posts: 172
Joined: Wed Sep 25, 2013 4:09 am

Re: Header why is it not working????

Post by hybris »

im on a webhotel so i dont have access to server logs...
edit ok so it didnt work in incognito either..
hybris
Forum Contributor
Posts: 172
Joined: Wed Sep 25, 2013 4:09 am

Re: Header why is it not working????

Post by hybris »

Ok so i found the error...
Read if you like because this was a freak error that could have taken me years to find out. I was just damn lucky and I still don't know why i fixed the problem only how i fixed the problem. Might save you time some day if you have a header error...

So as a part of the login system i have a connect to database file. I also have a settings file that defines HOST and so on.
In the settings file at the very end of the file after the ?> i had 2 empty blank lines and those were the cause for header not to work in the other page.

Since I on the header page included a functions file that in turn included the settingsfile the empty lines messed up my header

thats why the header file worked when i moved it on top of the page but not in the if block (after the include).

I still don't know why those empty lines messed everything up since i have blank lines in the rest of the code.

My guess (and gut feeling) is that since they were at the end of the file after the ?> they were interpret as HTML as show 2 blank lines on screen (which would be similar as an echo command before I called header). That would also explain why my function worked when i replaced header with echo since echo is unsensitive for previous echo but header is not.

Damn call me Sherlok Holmes from now on ;)
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Header why is it not working????

Post by Celauran »

That's actually quite common, though it generally results in a "headers already sent" error. Might want to double check your error reporting settings.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Header why is it not working????

Post by Christopher »

hybris wrote:So as a part of the login system i have a connect to database file. I also have a settings file that defines HOST and so on.
In the settings file at the very end of the file after the ?> i had 2 empty blank lines and those were the cause for header not to work in the other page.
FYI - it is a PHP best practice to not put the closing ?> at the end of files that contain only PHP code -- for just this reason.
(#10850)
hybris
Forum Contributor
Posts: 172
Joined: Wed Sep 25, 2013 4:09 am

Re: Header why is it not working????

Post by hybris »

Aha,

I thought I was really smart when I found this lol :) Didn't know it was a common issue. Well I guess you wont have to call me Sherlock then haha.

About the not putting the end ?> in pure php files I had no clue it was best practice, I seen it in lots of examples on the internet but I thought it was because the author was sloppy and tried to save a 10:th of a second by not typing it out since the code would work anyway.

Thanks again for helping me, I learned a lot about php from this place and I hope in the future I will be good enough to help others. For now the best thing I can do is to post the solution to my own questions at the end in my threads :)

So lession learned: Skip the ?> at end of pure php files since it may very well be the root of all evil (together with that fuzzy thing growing under my sofa after my vacuumcleaner died, but thats another story) especially if you plan to use header(location: ) function within your project :D
Post Reply