Jump to content

Recommended Posts

hi. im new here as im currently learning php and thought this might be a good place to get some help on the things im unsure of.

 

i want to make a web site with only one page but instead of the page changeing when i click on a link i want a php include to change or some other method of changing the page content. the method im currently using is the include is as followes.

 

include "$page.php";

 

This variable is then (or rather is is supposed to but doesnt...) be changed when i click on a link as follows,

 

<a href="mysite.com/welcome.php?page=secondpage"> then a picture is here instead of text...

 

This doesnt seem to work... any ideas as to where im going wrong?

Link to comment
https://forums.phpfreaks.com/topic/140670-one-site-one-page/
Share on other sites

<?php

Switch($_GET['page']){

default:
	echo("Homepage. <a href='./?page=2'>Page 2</a>. <a href='./?page=guestbook'>Guestbook</a>");
break;

case 2:
	echo("<a href='./?'>Homepage</a>. Page 2. <a href='./?page=guestbook'>Guestbook</a>.");
break;

case "guestbook":
	echo("<a href='./?'>Homepage</a>. <a href='./?page=2'>Page 2</a>. Guestbook.");
break;
}

?>

 

Be careful with input from clients, ie. anything in the url, or form data. ($_GET,$_POST,$_COOKIE,$_REQUEST) even some $_SERVER['REMOTE_ADDR'] type globals can be compromised;

Using predefined page names or numbers eliminates including or displayed malicious code.

 

Save the above code to test.php and load it up.

Link to comment
https://forums.phpfreaks.com/topic/140670-one-site-one-page/#findComment-736181
Share on other sites

Be sure to set a default in your switch statement. Bring it to a 404 folder. I often find that this style of website is easier to maintain by creating a folder for the processes of each page and a folder for the body of each page. That way you can easily just include files based on the $_GET value being received.

 

 

Link to comment
https://forums.phpfreaks.com/topic/140670-one-site-one-page/#findComment-736244
Share on other sites

<?php
$page = mysql_real_escape_string($_GET['page']);

if($page =="firstpage"){
include("firstpage.php");
}
if($page =="secondpage"){
include("secondpage.php");
}
?>

 

Why are you cleaning $_GET['page']? Unless you're planning on using $page in a query, there's absolutely no point in trying to escape it. Also, if there isn't a connection in existence, this will cause a nasty looking warning.

Link to comment
https://forums.phpfreaks.com/topic/140670-one-site-one-page/#findComment-736247
Share on other sites

<?php

$page = $_GET['page'];
if(file_exists($page.'.php'))
{
include($page.'.php');
}
else
{
echo 'Page not found.';
}

?>

 

You could use that. But then your url's will need to either be:

 

http://www.domain.com/?page=firstpage

 

OR

 

http://www.domain.com/index.php?page=firstpage

 

And you need to use the according file name without the .php or .txt or .html or what so ever.

Link to comment
https://forums.phpfreaks.com/topic/140670-one-site-one-page/#findComment-736248
Share on other sites

<?php

$page = $_GET['page'];
if(file_exists($page.'.php'))
{
include($page.'.php');
}
else
{
echo 'Page not found.';
}

?>

 

You could use that. But then your url's will need to either be:

 

http://www.domain.com/?page=firstpage

 

OR

 

http://www.domain.com/index.php?page=firstpage

 

And you need to use the according file name without the .php or .txt or .html or what so ever.

 

To add to this, use basename on the get data, to avoid them changing directories on you:

<?php
$page = basename($_GET['page']);

if(file_exists($page.'.php'))
{
   include($page.'.php');
}
else
{
   echo 'Page not found.';
}

?>

 

That will be more secure.

Link to comment
https://forums.phpfreaks.com/topic/140670-one-site-one-page/#findComment-736282
Share on other sites

Let's just say by using your script..

 

We have 100 page's.

 

100 x 3 => 300.

 

300 Line's of code. To find fiftyforthpage.php and remove it. Very self complicated. And time taking.

 

Using:

 

<?php
$page = basename($_GET['page']);
if( file_exists($page.'.php') )
{
   include($page.'.php');
}
else
{
   echo 'Page not found';
}
?>

 

I think you have a better chance at removing a link as simple as removing the link from the place you want to take it away and removing the file.

 

You could even restrict page's by using this code:

 

$errno = array('test','call'); //add more to it if u like
if( in_array($errno, $page.'.php') )
{
   echo 'Page currently disabled.';
}

 

Just my 2 cent's.

Link to comment
https://forums.phpfreaks.com/topic/140670-one-site-one-page/#findComment-736476
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.