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!

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

$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

 

 

 

Link to comment
Share on other sites

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;

}

 

?>

Link to comment
Share on other sites

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);

}

 

?>

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.