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.

transfer to 404 with php - or something like that anyway

Discussion in 'PHP' started by miko67, Jan 15, 2006.

  1. #1
    I have a wordpress index.php that runs through this code:

    My problem is, that when I get to the part of the loop where there is no posts to post (ie. There is a faulty URI or a dead link), wordpress just serves me this line ('Sorry, no posts matched your criteria), as a integrated part of a wordpress theme.

    This is no good, because there needs to be a header in the 404 page saying something like this:

    And here is my callenge and job-to-do:

    I (think that I) need to make some kind of “IF” statement in the top of the header or before the header saying something like “if this is a faulty URI or if no posts meet your criteria, then go to 404.php and present that”

    I have tried to mix up som php for that but couldn’t get it to work.

    Is there anybody out there who can figure this one out?

    Help is really appreciated.
     
    miko67, Jan 15, 2006 IP
  2. execute

    execute Peon

    Messages:
    301
    Likes Received:
    12
    Best Answers:
    0
    Trophy Points:
    0
    #2
    <?phpif(have_posts()):while(have_posts()): the_post();?>

    what the hell kinda short hand code is that?
    I've never seen such a messy code, and i cant even tell what it does.
    if(): <--- wrong should give you error, never see nthat before.
    while(): <--- wrong again, should give you error, if it doesn't then im crazy...

    man i dont even know if thats PHP or ASP??
     
    execute, Jan 15, 2006 IP
  3. WoodyRoundUp

    WoodyRoundUp Peon

    Messages:
    1
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    _e('Sorry, no posts matched your criteria.');

    Modify this part to:

    echo "<script type=\"text/javascript\">window.location.href='YOUR 404 PATH';</script>";
     
    WoodyRoundUp, Jan 15, 2006 IP
  4. miko67

    miko67 Well-Known Member

    Messages:
    769
    Likes Received:
    59
    Best Answers:
    0
    Trophy Points:
    120
    #4
    This solution is a no-go. It will still try to put some content (the 404) on the main content place, an not make a switch to the 404 page.

    Besides this solution will give me a "header already sent" error, and I need to send 404 header information when a page is not found.
     
    miko67, Jan 16, 2006 IP
  5. miko67

    miko67 Well-Known Member

    Messages:
    769
    Likes Received:
    59
    Best Answers:
    0
    Trophy Points:
    120
    #5
    Hrmmm, I am lost for words here. What do I need that for?

    If you don't have anything constructive to say...:eek:

    I'd be happy for some pointers though.
     
    miko67, Jan 16, 2006 IP
  6. mitchandre

    mitchandre Peon

    Messages:
    313
    Likes Received:
    33
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Is that php code?
     
    mitchandre, Jan 16, 2006 IP
  7. miko67

    miko67 Well-Known Member

    Messages:
    769
    Likes Received:
    59
    Best Answers:
    0
    Trophy Points:
    120
    #7
    Supposedly... any pointers?
     
    miko67, Jan 16, 2006 IP
  8. stuw

    stuw Peon

    Messages:
    702
    Likes Received:
    44
    Best Answers:
    0
    Trophy Points:
    0
    #8
    This is shorthand similar to
    <?=(test ? true : false)?>
    eg
    <?=($x==1 ? 'x equals one' : 'x does not equal one')?>
    same as
    if ($x==1) {
    echo 'x equals one';
    } else {
    echo 'x does not equal one';
    }
     
    stuw, Jan 16, 2006 IP
    miko67 likes this.
  9. stuw

    stuw Peon

    Messages:
    702
    Likes Received:
    44
    Best Answers:
    0
    Trophy Points:
    0
    #9
    maybe you could buffer your output, and then if there are no posts send the redirect headers....
     
    stuw, Jan 16, 2006 IP
  10. miko67

    miko67 Well-Known Member

    Messages:
    769
    Likes Received:
    59
    Best Answers:
    0
    Trophy Points:
    120
    #10
    sounds about right. I'll give it a try. I don't suppose you have anything resembling a piece of code example that I could study?
     
    miko67, Jan 16, 2006 IP
  11. stuw

    stuw Peon

    Messages:
    702
    Likes Received:
    44
    Best Answers:
    0
    Trophy Points:
    0
    #11
    It's not something I'ved one myself. But I think it's quite straight forward.
    Use ob_start() to start buffing content and ob_nd_flush() to send it to the browser
     
    stuw, Jan 16, 2006 IP
  12. miko67

    miko67 Well-Known Member

    Messages:
    769
    Likes Received:
    59
    Best Answers:
    0
    Trophy Points:
    120
    #12
    stuw, you are :cool: The Man :cool:

    Thx a lot for seeing through this problem of mine. All I ever needed was to do exactly what you told me:

    Line 1:
    • <?php ob_start(); ?>
    Line (somewhere in the main body where it was needed):
    • <?php header("HTTP/1.1 404 Not Found");?>
    • <?php ob_end_flush(); ?>
    • <?php $page = file_get_contents('404.php');
    • echo $page;?>
    as I needed to get the header 404 not found sentence in the header if there were no posts or an invalid URI.

    My Google sitemap verified with Google now. Very Cool indeed.
    *happy-man-humming-all-day*
     
    miko67, Jan 17, 2006 IP
  13. stuw

    stuw Peon

    Messages:
    702
    Likes Received:
    44
    Best Answers:
    0
    Trophy Points:
    0
    #13
    thanks for posting your code. I use wordpress myself so this will come in handy.
     
    stuw, Jan 17, 2006 IP
  14. execute

    execute Peon

    Messages:
    301
    Likes Received:
    12
    Best Answers:
    0
    Trophy Points:
    0
    #14
    USually short hand causes errors... That's why i posted what i posted.
    Re-code it, neatly, correctly, not short-hand for extra mistakes.
     
    execute, Jan 27, 2006 IP
  15. buyallgifts

    buyallgifts Peon

    Messages:
    583
    Likes Received:
    17
    Best Answers:
    0
    Trophy Points:
    0
    #15
    I am having a similar problem getting Google to verify my site. But I am not using php scripts. Any suggestions.

    http://www.bollywoodmovies.us
     
    buyallgifts, Apr 11, 2006 IP
  16. miko67

    miko67 Well-Known Member

    Messages:
    769
    Likes Received:
    59
    Best Answers:
    0
    Trophy Points:
    120
    #16
    Only one suggestions... Get around to doing php. It ought to be available to you.:)
     
    miko67, Apr 11, 2006 IP
  17. exam

    exam Peon

    Messages:
    2,434
    Likes Received:
    120
    Best Answers:
    0
    Trophy Points:
    0
    #17
    // Rant # 1 coming....

    I disagree. Neatly is subjective, "not short-hand" is not necessarily going to be easier to read, and "correctly", well, it is correct- otherwise it'd spit back a syntax error. You cannot generalize and say
    if (condition) {
    
    } else {
    
    }
    PHP:
    is the best syntax. A better suggestion would be to use the same syntax consistently throughout an entire project (Which wordpress does.)

    // Rant # 2 coming...
    I can't help but correct you. The wordpress code is actually:
    if (condition):
    
    else:
    
    endif;
    PHP:
    Which is simply an alternative to
    if (condition) {
    
    } else {
    
    }
    PHP:
    and it is different from this setup
    condition ? if_true_result : if_false_result;
    PHP:
    The main difference being that the last construct returns the resulting value, the other 2 do not- not an insignificant difference.

    Thanks for hearing me out :)
     
    exam, Apr 11, 2006 IP
  18. stuw

    stuw Peon

    Messages:
    702
    Likes Received:
    44
    Best Answers:
    0
    Trophy Points:
    0
    #18
    woooah drink some coffe dude, or take a chill pill! ;)

    Actually I agree with you on point 1

    As for point 2 I wasn't trying to explain the logic - just that short hand notation existed
     
    stuw, Apr 11, 2006 IP