Web Hosting Talk







View Full Version : Help with PHP "Switch" function please


latheesan
07-26-2005, 06:23 AM
This is how i laid out my index.php page

<?php

include ("header.php");

switch ($id) {

case "login":
include ("login.php");
break;

case "register":
include ("register.php");
break;

default:
include ("home.php");
break;
}

include ("footer.php");

?>

So if i want to go to the login page, i would type the link like this

http://www.mysite.com/index.php?id=login

my problem is that, how do i stop people from entering login page like this "http://www.mysite.com/login.php" because on the login page, its only a table. it has no css, header or footer and so on.

Please help, and any help anyone can offer is most appreciated

hiryuu
07-26-2005, 06:31 AM
Option 1: Set a constant in index.php and have the others redirect to index.php if the constant isn't defined.
Option 2: Check the REQUEST_URI to see what they requested and redirect as above.
Option 3: Use .htaccess (probably mod_rewrite) to trap everything except index.php, and handle it from there.

I recommend #1, since it gives you a dependable and flexible way to handle direct requests, even as you move files around.

dollar
07-26-2005, 06:33 AM
Well the easiest option would be to move login.php to a directory people would not think to look, such as inc/templates, and then call it as

include('inc/templates/login.php');

then you could create a login.php page and use something like

header("Location: index.php?id=login");

latheesan
07-26-2005, 07:41 AM
erm justadollarhostin, i tried your method and it quite didnt work out the way i expected it to be

is there a way i can set something in the login.php that if user enters the url directly, they get re-drected to the index page

u know how the modules in the php-nuke works,

if u enter the url http://www.mysite.com/modules.php?name=Your_Account (it works)

but if u enter the url like this

http://www.mysite.com/modules/Your_Account/index.php (it wont work, it says u cant access this file directly)

i wanna be able to do something similar to that

dollar
07-26-2005, 07:52 AM
Do what was mentioned by hiryuu above then. Delare a constant in your index.php file and then echo You can't access the file directry if that variable is not passed.

ie.

in index.php

define("IN_SCRIPT", "true");

in login.php

<?
if ( !defined('IN_SCRIPT') )
{
echo("
?>
<html>
<head>
<meta http-equiv="refresh" content="2;url=http://yourhost.com/index.php">
</head>
<body>
<div align="center">Sorry, you can not access this file directly</div>
</body>
</html>
<?
");
} else {
echo("
?>
Login Page Stuff here that will be echoed
<?
");
} //end if
?>


That code was not tested, just off the top of my head.

What was wrong with the code I gave you the first time? If you just needed to change where it redirected all you need to do is edit the

header("Location: index.php?id=login");

line.

latheesan
07-26-2005, 08:48 AM
What was wrong with the code I gave you the first time? If you just needed to change where it redirected all you need to do is edit the

well, when i entered the index.php page, it was blank, now let me try ur second method, it seems better and easier to understand

latheesan
07-26-2005, 08:52 AM
Thank you justadollarhostin, Your method worked just fine :)

dollar
07-26-2005, 05:23 PM
Anytime ;)