1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

PHP Sessions not working with header redirect?

Discussion in 'PHP' started by samie, Dec 13, 2013.

  1. #1
    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.
     
    samie, Dec 13, 2013 IP
  2. hdewantara

    hdewantara Well-Known Member

    Messages:
    536
    Likes Received:
    47
    Best Answers:
    25
    Trophy Points:
    155
    #2
    The file format, probably?
    When with Notepad++, I'll always make sure the encoding is in UTF-8, and EOL isn't Mackintosh.
     
    hdewantara, Dec 13, 2013 IP
    samie likes this.
  3. samie

    samie Member

    Messages:
    269
    Likes Received:
    8
    Best Answers:
    1
    Trophy Points:
    45
    #3
    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 :p Thanks again for the help!
     
    samie, Dec 13, 2013 IP
  4. scintillescent_dude

    scintillescent_dude Greenhorn

    Messages:
    25
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    11
    #4
    use ob_start to stop this error
     
    scintillescent_dude, Dec 14, 2013 IP
  5. samie

    samie Member

    Messages:
    269
    Likes Received:
    8
    Best Answers:
    1
    Trophy Points:
    45
    #5
    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.
     
    samie, Dec 16, 2013 IP
  6. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,998
    Best Answers:
    253
    Trophy Points:
    515
    #6
    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...
     
    deathshadow, Dec 18, 2013 IP