...

View Full Version : error 404 if page is not on list in txt file



young_coder
02-18-2010, 03:14 PM
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!

Fou-Lu
02-18-2010, 03:23 PM
Yep


<?php
session_start();
$sFilePath = '/path/to/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);

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

?>


Untested, but something like that?


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.

young_coder
02-18-2010, 03:54 PM
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

Fou-Lu
02-18-2010, 04:03 PM
<?php
session_start();
$sFilePath = '/path/to/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", 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.

young_coder
02-18-2010, 04:25 PM
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

Fou-Lu
02-18-2010, 04:37 PM
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?


Also, next time can you view the source code and copy the output? That way we keep it on separate lines.

young_coder
02-18-2010, 05:04 PM
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

Fou-Lu
02-18-2010, 05:17 PM
Ok, add one more printf, I was mistaken on my assumption that was actually a part of the name:


printf("In \$aValids: %s\n", implode(', ', $aValids));



Also, add one test for one thats invalid, and one that is valid.

young_coder
02-18-2010, 05:26 PM
Sorry because I badgering you but I'm newbie with PHP :o
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

Fou-Lu
02-18-2010, 05:36 PM
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
session_start();
$sFilePath = './pages.txt';
$_SESSION['raw_query'] = $_GET['q'];
$_SESSION['ad_group'] = strtolower(str_replace("-", " ", $_GET['q']));

$aValids = @file($sFilePath, FILE_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();

}

?>

young_coder
02-18-2010, 05:44 PM
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

Fou-Lu
02-18-2010, 06:15 PM
Can you post the pages.txt file as an attachment please?

young_coder
02-18-2010, 06:25 PM
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 :(

Fou-Lu
02-18-2010, 06:44 PM
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
session_start();
$sFilePath = './pages.txt';
$_SESSION['raw_query'] = $_GET['q'];
$_SESSION['ad_group'] = strtolower(str_replace("-", " ", $_GET['q']));

$aValids = @file($sFilePath, FILE_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.

young_coder
02-18-2010, 07:40 PM
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;
}
?>

Fou-Lu
02-19-2010, 12:17 AM
Glad you got it working!
I'm not sure what you're troubles were, I used this and it worked fine:


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

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


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

print 'the provided page ' . $_SESSION['ad_group'] . ' was found.';

?>


Only thing I should mention is that my handling for errors is poor in this. I don't normally recommend ever passing an array index by reference, but at least this way we won't get errors.

Also, if you're not aware of this, I don't believe firefox has a default 404 page. So passing a header for a 404 will just be blank, you'll need to control you're own error documents from there. Testing in IE provides a default 404 page.

young_coder
02-21-2010, 02:51 PM
Thank you in all your help. :)
I didn’t know that about FireFox
Do you think this is OK for all browsers?

if ($found == 'no')
{
header( 'Location: 404.html' );
exit;
}



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum