Jump to content


dynamic url problem


4 replies to this topic

#1 romer

    Young Padawan

  • Members
  • Pip
  • 19 posts

Posted 27 August 2009 - 02:40 PM

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.

$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?

#2 derek.sullivan

    Jedi In Training

  • Members
  • PipPip
  • 341 posts
  • Gender:Male
  • Location:Georgia
  • Interests:preaching, programming, music, friends, outdoors, moves, books

Posted 27 August 2009 - 03:38 PM

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:

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

include($n.".php");

}else{

die("error!");

}

?>


#3 romer

    Young Padawan

  • Members
  • Pip
  • 19 posts

Posted 27 August 2009 - 08:42 PM

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

Edited by romer, 27 August 2009 - 08:45 PM.


#4 derek.sullivan

    Jedi In Training

  • Members
  • PipPip
  • 341 posts
  • Gender:Male
  • Location:Georgia
  • Interests:preaching, programming, music, friends, outdoors, moves, books

Posted 28 August 2009 - 09:04 AM

if ($i != 'error') try this instead

if ($i !== 'error')

== is equal to.

#5 rc69

    PHP Master PD

  • P2L Staff
  • PipPipPipPip
  • 3,827 posts
  • Gender:Male
  • Location:Here
  • Interests:Web Development

Posted 28 August 2009 - 10:15 AM

View Postderek.sullivan, on Aug 27 2009, 01:38 PM, said:

<?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/manua...ction.isset.php

View Postderek.sullivan, on Aug 28 2009, 07:04 AM, said:

if ($i != 'error') try this instead

if ($i !== 'error')

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

Ref: http://php.net/manua....comparison.php

What i would recommend doing is this:
$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.





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users