header problem

Discussion in 'PHP' started by greatlogix, Aug 1, 2007.

  1. #1
    what's wrong with this code.
    
    if( urldecode($cat) == 'crib / toddler sheets'){ 
         header("HTTP/1.1 301 Moved Permanently"); 
         header("Location: http://www.mysite.com/mypage.php?pcat=21");
        echo "abc";
    }
    
    PHP:
    I can see 'abc' printed on the page when I click this URL "mysite.com/product_listing.php?cat=crib+%2F+toddler+sheets". What should I do to redirect to mypage.php?pcat=21
     
    greatlogix, Aug 1, 2007 IP
  2. jestep

    jestep Prominent Member

    Messages:
    3,659
    Likes Received:
    215
    Best Answers:
    19
    Trophy Points:
    330
    #2
    Try this:

    
    if( urldecode($cat) == 'crib / toddler sheets'){
         header("HTTP/1.1 301 Moved Permanently");
         header("Location: http://www.mysite.com/mypage.php?pcat=21");
         exit();
    echo "abc";
    }
    
    PHP:
     
    jestep, Aug 1, 2007 IP
  3. greatlogix

    greatlogix Active Member

    Messages:
    664
    Likes Received:
    13
    Best Answers:
    1
    Trophy Points:
    85
    #3
    exit() did not work. blank page. Infact header() function is not doing it's job.
     
    greatlogix, Aug 1, 2007 IP
  4. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #4
    Set these lines on top.

    
    error_reporting(E_ALL);
    ini_set('display_errors', '1');
    
    PHP:
     
    nico_swd, Aug 1, 2007 IP
  5. greatlogix

    greatlogix Active Member

    Messages:
    664
    Likes Received:
    13
    Best Answers:
    1
    Trophy Points:
    85
    #5
    I have tried this. There is no Warning or Notice on the page. It is very strange type of error. I don't know how to fix it. :mad:
     
    greatlogix, Aug 1, 2007 IP
  6. Brewster

    Brewster Active Member

    Messages:
    489
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    60
    #6
    Where are you getting the value of $cat from ? Can you post the code that is above the code that you have posted.

    Brew
     
    Brewster, Aug 1, 2007 IP
  7. Brewster

    Brewster Active Member

    Messages:
    489
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    60
    #7
    Try this:

    if( urldecode($_GET['cat']) == 'crib / toddler sheets'){
         header("HTTP/1.1 301 Moved Permanently");
         header("Location: http://www.mysite.com/mypage.php?pcat=21");
        echo "abc";
    }
    PHP:
    Brew
     
    Brewster, Aug 1, 2007 IP
  8. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #8
    nico_swd, Aug 1, 2007 IP
  9. Brewster

    Brewster Active Member

    Messages:
    489
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    60
    #9
    Good point. How about echoing the result of urldecode($_GET['cat'])

    That may give a clue where to look next.

    Brew
     
    Brewster, Aug 1, 2007 IP
  10. greatlogix

    greatlogix Active Member

    Messages:
    664
    Likes Received:
    13
    Best Answers:
    1
    Trophy Points:
    85
    #10
    This code is at top of the page. only database connection and session include file is above it.
    nico_swd is right $_GET is not a problem. Problem is in header function. It's not working.
     
    greatlogix, Aug 1, 2007 IP
  11. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #11
    Just out of curiosity, what does this give you:
    
    if (headers_sent())
    {
        echo 'Headers sent!';
    }
    
    PHP:
     
    nico_swd, Aug 1, 2007 IP
  12. greatlogix

    greatlogix Active Member

    Messages:
    664
    Likes Received:
    13
    Best Answers:
    1
    Trophy Points:
    85
    #12
    Good Point. I am always getting 'Headers sent!' message. Now I put the following code on the top of the page above the connection include file. Now following lines are first 6 lines of page.
    
    <?php if (!headers_sent()){
    if($pcat == 5){
    		header( "HTTP/1.1 301 Moved Permanently" ); 
    		header("Location: mypage.php?pcat=38"); echo "abc";
    	 }
    }	 
    
    PHP:
    now i am not getting 'abc' printed on the screen.

    It means header sent but what headers were sent?
     
    greatlogix, Aug 1, 2007 IP
  13. Brewster

    Brewster Active Member

    Messages:
    489
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    60
    #13
    There is a plugin for Firefox called Live Headers that will tell you what headers are sent.

    Brew
     
    Brewster, Aug 1, 2007 IP
  14. DavidAusman

    DavidAusman Well-Known Member

    Messages:
    399
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    108
    #14
    Maybe your register global is off. Try
    
    if($_GET['pcat'] == 5) { echo "abc"; }
    
    PHP:
     
    DavidAusman, Aug 1, 2007 IP
  15. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #15
    ^^ No. Has been discussed earlier.


    
    
    echo '<pre>' . print_r(get_headers('http://your-page.com/'), true) . '</pre>';
    
    PHP:
    Put the URL of the page you're having problems with there and see what headers you get.
     
    nico_swd, Aug 1, 2007 IP
  16. greatlogix

    greatlogix Active Member

    Messages:
    664
    Likes Received:
    13
    Best Answers:
    1
    Trophy Points:
    85
    #16
    I am getting Fatal Error:
    Fatal error: Call to undefined function: get_headers()

    I have also installed Firefox plugin but it's not helping. I am getting long list of headers.
     
    greatlogix, Aug 1, 2007 IP
  17. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #17
    Could you post a screenshot of that list?

    And also, have a look if there's maybe a white space before the PHP opening tag. <?php
     
    nico_swd, Aug 1, 2007 IP
  18. greatlogix

    greatlogix Active Member

    Messages:
    664
    Likes Received:
    13
    Best Answers:
    1
    Trophy Points:
    85
    #18
    No there is no space before <?php tag.

    Following are the headers by header plugin
     
    greatlogix, Aug 1, 2007 IP
  19. greatlogix

    greatlogix Active Member

    Messages:
    664
    Likes Received:
    13
    Best Answers:
    1
    Trophy Points:
    85
    #19
    Dear friends thanks for your time and help. I have found a solution.

    put ob_start(); on the top of the page and ob_end_flush(); should be last line of code. Now its working but main question is still there that what was the problem with header() function.
     
    greatlogix, Aug 3, 2007 IP