Hi there, I've been having a big issue with trying to use a header redirect within a session, and wondering if you guys have any advice on what could be going on. <?php session_start(); if (isset($_SESSION['redirect'])){ header('Location: http://randomsite.com/'); } ?> <html> <head> (the rest of the page) PHP: In my error logs I keep getting an error that header output is already sent on line 1. But I've checked and there's no spaces or extra links or anything. All the HTML shows as if the header redirect wasn't there. I don't understand why it's not working. The session is created on a previous page and I know if statement is being executed because firstly the error logs are being triggered, and also I can echo text within the IF statement and that will show on the page. Just as a test anyways. I've also tried adding quit and exit underneath the header redirect and this just seems to result in a blank page. Any suggestions? I'm hoping I typed the above information correctly because I'm just going by memory.
The file format, probably? When with Notepad++, I'll always make sure the encoding is in UTF-8, and EOL isn't Mackintosh.
Thank you for the help! I think that fixed it. It's strange.. but I use Notepad++ and both PHP files are already on UTF-8. But I noticed when uploading via Filezilla, the file sizes are changing right after it's uploaded. So say one of the files is 127 bites, then suddenly is changes to 123 after it's uploaded. So not too sure what's going on, but maybe it's something to do with that fact I first made the files using just plain old notepad. Or maybe it's something with Firefox. But I even deleted everything from the server and re-uploaded and it still wasn't working. I ended up having to just login to file manager with my host and opening and resaving the files. Anyways.. strange Thanks again for the help!
I had already previously tried that, but made no difference. However, after opening up and re-saving the file within File Manager (through my Web Host), it now appears to be work for me. It had something to do with the file format / encoding of the file like hdewantara stated.
Are you redirecting to the same domain? Remember, sessions use cookies, and cookies are domain specific! Is your php file above being called directly, or from some other file? Really it's hard to diagnose what could be going on from this tiny snippet... though your mentioning the editor and load/save probably means your UTF-8 has a BOM... which can even screw up PHP (sad as that is). You dropped the BOM on me, baby... you dropped the BOM on me...