Help - Search - Members - Calendar
Full Version: dynamic url problem
Pixel2Life Forum > Help Section > PHP, ASP, MySQL, JavaScript and other Web/Database Programming Help
romer
im useing script to turn mysite.com/downoads.php to mysite.com/index.php?n=downloads
it work fine all except the default page. it always stays there no matter what page ur on.so below my downloads the news is still displayed.

CODE
$n = isset($_GET['n']) ? $_GET['n'] : null;

if($n) {
    try {
        include $n.'.php';
    } catch (Exception $e) {
        header("Location: ".$_SERVER['PHP_SELF']."?i=error");
    }
}

if($i != 'error')
    include 'modules/news.php';

what am i doing wrong?
derek.sullivan
use the isset in the if statement, or just do the if statement if ($n) {} and get rid of isset($_GET['n']) ? $_GET['n'] : null; not sure what that is for...

and get rid of the try and catch statement

simplicity is key like this:

CODE
<?php
$n = trim($_GET['n']);
if ($n) { // or if (isset($n))

include($n.".php");

}else{

die("error!");

}

?>
romer
ok how do i set the main page that displays by default when they go to index.php.that is the problem im having the script works fine it just always shows the default page after the page that it calls
derek.sullivan
if ($i != 'error') try this instead

CODE
if ($i !== 'error')


== is equal to.
rc69
QUOTE (derek.sullivan @ Aug 27 2009, 01:38 PM) *
CODE
<?php
$n = trim($_GET['n']);
if ($n) { // or if (isset($n))

include($n.".php");

}else{

die("error!");

}

?>

Note, you should be careful using isset() as it will return true as long as the variable is set to something other than null (i.e. an empty string).
Ref: http://php.net/manual/en/function.isset.php

QUOTE (derek.sullivan @ Aug 28 2009, 07:04 AM) *
if ($i != 'error') try this instead

CODE
if ($i !== 'error')


== is equal to.

!= is not equal to, !== is not identical to.

Ref: http://php.net/manual/en/language.operators.comparison.php

What i would recommend doing is this:
CODE
$n = str_replace('.', '', trim($_GET['n']));
if(file_exists($n)){
    include $n.'.php';
}else{
    include 'modules/news.php';
}

If that doesn't work, you should make sure the path $n.'.php' points to the correct file.
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2009 Invision Power Services, Inc.