Jump to content

error 404 if page is not on list in txt file


young_coder

Recommended Posts

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!

I don't have time to write out the code but here is my input in theory.

 

Take the _GET input and put it into a var.

Do a for each loop on each line of the text file and compare it against the var.

On success set a trigger var to 1

On failure the trigger var will remain 0

At the end of the code check if trigger var is 1 if so do whatever.

if not goto a predefined 404 page.

 

If you didn't understand or you need help with the code,leave a comment and i'l write it out when i get back in a few hours.

$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;
}

 

*or something like that depending on how your controller page is set up to display content.

 

 

HTH

Teamatomic

 

 

 

One more time thank you on help  :)

Code is very simple and its just this

 

<?php

session_start();

$_SESSION['raw_query'] = $_GET['q'];

$_SESSION['ad_group'] = ucwords(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;

}

 

?>

If I put like this, than is no error 404

 

 

<?php

session_start();

$_SESSION['raw_query'] = $_GET['q'];

$_SESSION['ad_group'] = ucwords(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 ($line == $str)

{

$found='yes';

end($lines);

}

 

?>

The way you are set up and from the code you displayed in your first post you must call your index page like this:

index.php?q=about-us

 

are you?

if not you must set up a conditional(if statement) to check for the presence of $_GET[q] or it will always expect it and so then without it will not match something form the pages.txt file.

 

 

HTH

Teamatomic

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.