Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 1 of 2 12 LastLast
Results 1 to 15 of 17
  1. #1
    New Coder
    Join Date
    Feb 2010
    Posts
    38
    Thanks
    14
    Thanked 0 Times in 0 Posts

    error 404 if page is not on list in txt file

    Hey guys!
    I have this little script saved like index.php

    <?php
    session_start();
    $_SESSION['raw_query'] = $_GET['q'];
    $_SESSION['ad_group'] = ucwords(str_replace("-", " ", $_GET['q']));

    echo $_SESSION['ad_group'];
    ?>


    and I have file pages.txt with name of every page like this:


    about us
    contact us
    site map


    It is possible when URLs "./index.php?q=about-us", "./index.php?q=contact-us", "./index.php?q=about-us" and so on are required by browser than script to go and check from pages.txt is there line with "about us", "contact us", "site map" etc, and if page is not in list to show error 404 page?
    Would appreciate some help with this one and thank you advance
    Cheers!

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    Yep
    PHP Code:
    <?php 
    session_start
    ();
    $sFilePath '/path/to/pages.txt';
    $_SESSION['raw_query'] = $_GET['q'];
    $_SESSION['ad_group'] = ucwords(str_replace("-"" "$_GET['q']));

    $aValids = @file($sFilePathFILE_IGNORE_NEW_LINES FILE_SKIP_EMPTY_LINES);

    if (!
    $aValids || !in_array($_SESSION['ad_group'], $aValids))
    {
        
    header("HTTP/1.0 404 Not Found");
        exit();
    }

    ?>
    Untested, but something like that?

    Edit:
    Actually, a better approach would be to simply store these in the session as well, rather than look up the file every time. But this depends on how much data we're talking about, give or take how many entries are there in pages.txt? 10? 100? 1000? Once you get to 1000+, I wouldn't want to be loading this array over and over. Heck, even 100+ is starting to push it, if we're talking about that kind of data, either a database is needed, or we need to scan a file and dump what it doesn't use. Oh, we could even write a random access type file with a second file for indices! That would be neato.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

  • Users who have thanked Fou-Lu for this post:

    young_coder (02-18-2010)

  • #3
    New Coder
    Join Date
    Feb 2010
    Posts
    38
    Thanks
    14
    Thanked 0 Times in 0 Posts
    Thank you on this quick respond, unfortunately something is wrong… what ever URL I require in browser it don’t shows me error 404
    And I think there be no more than 500 entries in pages.txt
    Last edited by young_coder; 02-18-2010 at 03:57 PM.

  • #4
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    PHP Code:
    <?php  
    session_start
    (); 
    $sFilePath '/path/to/pages.txt'
    $_SESSION['raw_query'] = $_GET['q']; 
    $_SESSION['ad_group'] = ucwords(str_replace("-"" "$_GET['q'])); 

    $aValids = @file($sFilePathFILE_IGNORE_NEW_LINES FILE_SKIP_EMPTY_LINES); 

    printf("\$aValids valid? %d\n", ($aValids != false));
    printf("\$aValids entries: %d\n"count($aValids));
    printf("\$_SESSION['ad_group']: %s\n"$_SESSION['ad_group']);
    printf("%s in \$aValids? %d\n"in_array($_SESSION['ad_group'], $aValids));

    if (!
    $aValids || !in_array($_SESSION['ad_group'], $aValids)) 

    //    header("HTTP/1.0 404 Not Found"); 
    //    exit(); 


    ?>
    Run that, view the source and post the result.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

  • Users who have thanked Fou-Lu for this post:

    young_coder (02-18-2010)

  • #5
    New Coder
    Join Date
    Feb 2010
    Posts
    38
    Thanks
    14
    Thanked 0 Times in 0 Posts
    I can’t believe you are fast like this with responses.
    Thank you very much on help
    This is error that I got

    $aValids valid? 1 $aValids entries: 3 $_SESSION['ad_group']: About Us1
    Warning: printf() [function.printf]: Too few arguments in C:\wamp\www\001\index.php on line 12

  • #6
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    Oops, I biffed the printf there hah.
    Thats ok, I see what I need. $aValids is not empty, contains three entries, and has a match for $_SESSION['ad_group'].
    Try the same thing (you can change this last printf line to printf("%s in \$aValids? %d\n", $_SESSION['ad_group'], in_array($_SESSION['ad_group'], $aValids));, and run it with invalid data please?

    Edit:
    Also, next time can you view the source code and copy the output? That way we keep it on separate lines.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

  • #7
    New Coder
    Join Date
    Feb 2010
    Posts
    38
    Thanks
    14
    Thanked 0 Times in 0 Posts
    I changed that line and here is new output from source code

    $aValids valid? 1
    $aValids entries: 3
    $_SESSION['ad_group']: About Us1
    About Us1 in $aValids? 0

  • #8
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    Ok, add one more printf, I was mistaken on my assumption that was actually a part of the name:
    PHP Code:
    printf("In \$aValids: %s\n"implode(', '$aValids)); 
    Edit:
    Also, add one test for one thats invalid, and one that is valid.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

  • Users who have thanked Fou-Lu for this post:

    young_coder (02-18-2010)

  • #9
    New Coder
    Join Date
    Feb 2010
    Posts
    38
    Thanks
    14
    Thanked 0 Times in 0 Posts
    Sorry because I badgering you but I'm newbie with PHP
    My code in this moment looks like this

    <?php
    session_start();
    $sFilePath = './pages.txt';
    $_SESSION['raw_query'] = $_GET['q'];
    $_SESSION['ad_group'] = ucwords(str_replace("-", " ", $_GET['q']));

    $aValids = @file($sFilePath, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);

    printf("\$aValids valid? %d\n", ($aValids != false));
    printf("\$aValids entries: %d\n", count($aValids));
    printf("\$_SESSION['ad_group']: %s\n", $_SESSION['ad_group']);
    printf("%s in \$aValids? %d\n", $_SESSION['ad_group'], in_array($_SESSION['ad_group'], $aValids));
    printf("In \$aValids: %s\n", implode(', ', $aValids));

    if (!$aValids || !in_array($_SESSION['ad_group'], $aValids))
    {
    // header("HTTP/1.0 404 Not Found");
    // exit();

    }

    ?>


    and new error that I got for invalid is

    $aValids valid? 1
    $aValids entries: 3
    $_SESSION['ad_group']: About Us1
    About Us1 in $aValids? 0
    In $aValids: about us
    , contact us
    , site map


    and for valid URL is

    $aValids valid? 1
    $aValids entries: 3
    $_SESSION['ad_group']: About Us
    About Us in $aValids? 0
    In $aValids: about us
    , contact us
    , site map
    Last edited by young_coder; 02-18-2010 at 05:28 PM.

  • #10
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    Ah, you're format includes the newlines. Thats not right, it should have been removed with the FILE_IGNORE_NEW_LINES. I'll double check that.
    Hmm, not sure, well try this and post the results. Use 1 test with valid, 1 test with invalid.
    Also, the in_array won't work with case sensitivity, so I'm removing the ucfirst call.

    PHP Code:
    <?php 
    session_start
    (); 
    $sFilePath './pages.txt'
    $_SESSION['raw_query'] = $_GET['q']; 
    $_SESSION['ad_group'] = strtolower(str_replace("-"" "$_GET['q'])); 

    $aValids = @file($sFilePathFILE_IGNORE_NEW_LINES FILE_SKIP_EMPTY_LINES); 

    array_map('rtrim'$aValids);

    printf("\$aValids valid? %d\n", ($aValids != false)); 
    printf("\$aValids entries: %d\n"count($aValids)); 
    printf("\$_SESSION['ad_group']: %s\n"$_SESSION['ad_group']); 
    printf("%s in \$aValids? %d\n"$_SESSION['ad_group'], in_array($_SESSION['ad_group'], $aValids));
    printf("In \$aValids: %s\n"implode(', '$aValids)); 

    if (!
    $aValids || !in_array($_SESSION['ad_group'], $aValids)) 

    // header("HTTP/1.0 404 Not Found"); 
    // exit(); 



    ?>
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

  • #11
    New Coder
    Join Date
    Feb 2010
    Posts
    38
    Thanks
    14
    Thanked 0 Times in 0 Posts
    OK, I changed code and this are errors this time:

    this is invalid

    $aValids valid? 1
    $aValids entries: 3
    $_SESSION['ad_group']: about us1
    about us1 in $aValids? 0
    In $aValids: about us
    , contact us
    , site map


    this is valid

    $aValids valid? 1
    $aValids entries: 3
    $_SESSION['ad_group']: about us
    about us in $aValids? 0
    In $aValids: about us
    , contact us
    , site map

  • #12
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    Can you post the pages.txt file as an attachment please?
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

  • #13
    New Coder
    Join Date
    Feb 2010
    Posts
    38
    Thanks
    14
    Thanked 0 Times in 0 Posts
    Now in attachment there are two zip files with two different ways to this be done but any of them doesn’t work in this moment
    Attached Files Attached Files

  • #14
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    Hmm, I won't be able to do anything here at work. Unless someone else can pick this up and correct it, you'll need to wait until I get off work to look at it.
    The problem is a simple matter of the newlines in the pages.txt file. But they are clearly on individual lines, and even with FILE_IGNORE_NEW_LINES and an rtrim map (hmm, wait a minute........) it still has issues.

    Also, I just noticed I biffed the array_map (too used to array_walk O.o):
    PHP Code:
    <?php  
    session_start
    ();  
    $sFilePath './pages.txt';  
    $_SESSION['raw_query'] = $_GET['q'];  
    $_SESSION['ad_group'] = strtolower(str_replace("-"" "$_GET['q']));  

    $aValids = @file($sFilePathFILE_IGNORE_NEW_LINES FILE_SKIP_EMPTY_LINES);  

    $aValids array_map('rtrim'$aValids); 

    printf("\$aValids valid? %d\n", ($aValids != false));  
    printf("\$aValids entries: %d\n"count($aValids));  
    printf("\$_SESSION['ad_group']: %s\n"$_SESSION['ad_group']);  
    printf("%s in \$aValids? %d\n"$_SESSION['ad_group'], in_array($_SESSION['ad_group'], $aValids)); 
    printf("In \$aValids: %s\n"implode(', '$aValids));  

    if (!
    $aValids || !in_array($_SESSION['ad_group'], $aValids))  
    {  
    // header("HTTP/1.0 404 Not Found");  
    // exit();  

    }  

    ?>
    Try that one now.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

  • #15
    New Coder
    Join Date
    Feb 2010
    Posts
    38
    Thanks
    14
    Thanked 0 Times in 0 Posts
    This working... thank you guys

    <?php
    session_start();
    $_SESSION['raw_query'] = $_GET['q'];
    $_SESSION['ad_group'] = str_replace("-", " ", $_GET['q']);

    $str=$_SESSION['ad_group'];
    $lines=file('./pages.txt');
    $found='no';
    foreach($lines as $line)
    {
    $line=trim("$line");
    if ($line == $str){$found='yes';}
    }
    if ($found == 'no')
    {
    include("./404.php");
    exit;
    }
    ?>


  •  
    Page 1 of 2 12 LastLast

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •