if url does exist than else mishaps

Li1M0ST3Rz

I <3 Bianca
Sep 13, 2010
269
166
Alright hi guys i havent been one for ages, oh well but heres my problem i havent coded for a while so its a little rusty
2na4sFM.png

PHP:
$host2 = $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
        $link2 = array ('index.php', 'home.php?home', 'home.php?register', 'home.php?personal', 'home.php?contact', 'home.php?benefit', 'process.php');
    if($host2 == $link2)
        {
so i am basically doing is coding a code that if the url aint what the array has than lead to 404 page at the end of the page i added this:
PHP:
<?php
   }else{
     header('./404.php');
   }
?>
 

Markshall

Русский Стандарт
Contributor
Dec 18, 2010
2,637
2,389
Just use a .htaccess file.

Code:
ErrorDocument 404 /404.php

However, if you're adament about doing it in PHP, you want to use this approach ideally:

PHP:
<?php
$host2 = $_SERVER['PHP_SELF'] . $_SERVER['QUERY_STRING'];
$link2 = array('index.php', 'home.php?home', 'home.php?register');
if (in_array($host2, $link2)) {
  //display page content
} else {
  header('Location: ./404.php');
}
?>
 
Last edited:

Li1M0ST3Rz

I <3 Bianca
Sep 13, 2010
269
166
Don't understand your issue or what you're trying to do, can you provide the full snippet and elaborate?
so im basically making a code that it determine if the link is correct what array has in it and if its not it'll lead to a 404 page and code to big to fit here.
 

BIOS

ಠ‿ಠ
Apr 25, 2012
906
247
so im basically making a code that it determine if the link is correct what array has in it and if its not it'll lead to a 404 page and code to big to fit here.
Do all the links have to be home.php?etc ?

You could use rewrite rules to route these requests to their respective pages, then just check if the file exists, if not display 404.
 

BIOS

ಠ‿ಠ
Apr 25, 2012
906
247
Well yes it does i want the code cooperate how i set it to be
I don't really recommend this approach as it isn't very user or SEO friendly.

You could use something like /home, /register, etc. At the minute some of it doesn't really make sense, e.g. this: home.php?home
 

TesoMayn

Boredom, it vexes me.
Oct 30, 2011
1,482
1,482
You're shit at explaining what you want, so I'm assuming you want it to check the URI for a certain page, if it's in the array execute a code, if not go to a 404 page. If so that's not the best approach but I since you're a novice you don't know.

This will work

PHP:
$uri = trim($_SERVER['REQUEST_URI'], '/');
$pages = [
  'index.php',
  'home.php',
  'process.php'
];
if (in_array($uri, $pages) {
  // code here
} else {
  header('Location: 404.php');
}
 

Users who are viewing this thread

Top