View Full Version : php question?
Sammie
December 7th, 2007, 18:49
Hello
Well i am designing a website for a friend of mine, i have a page called enteredteams.php now what i am wanting to do is have an extension onto that such as enteredteams.php?page=create which will go to my createteam.php file.
I've seen it done on many websites but am needing a little bit of help on how i can do this?
Cheers
Sammie
krakjoe
December 7th, 2007, 19:38
You wouldn't normally actually use another file, observe ..
<!-- For the purposes of this post assume this is a full header, with style.css etc attached -->
<html>
<head>
<title>The page with no end</title>
</head>
<body>
<!-- For the purposes of this post, lets assume this is a formatted page body -->
<?php
/**
* We start switching a get variable, to find out what content to display
**/
switch( @$_GET['page'] )
{
/**
* Here we include a file, in my opinion the most suitable for the task at hand
**/
case 'prodbably_the_easiest_solution_for_you': include( "another.php" ); break;
/**
* Not always suitable but an option none the less
**/
case 'another_viable_option_but_a_silly_idea':
header( "Location: another.php" );
exit;
break;
/**
* More commonly, you would output html, or include template files or whatever
**/
case 'create':
?>
<table width="100%">
<tr>
<td>Create page content</td>
</tr>
</table>
<?php
break;
case 'another':
?>
<table width="100%">
<tr>
<td>Another page content</td>
</tr>
</table>
<?php
break;
default:
?>
<table width="100%">
<tr>
<td>Default page content</td>
</tr>
</table>
<?php
break;
}
?>
</body>
</html>
Get it ??
Sammie
December 7th, 2007, 21:15
Cheers, i've tested each one and know what they do, i will do more testing later with my friends website but is it possible to use the following but whilst keeping the .php?page=about?
<?php
/**
* We start switching a get variable, to find out what content to display
**/
switch( @$_GET['page'] )
{
/**
* Not always suitable but an option none the less
**/
case 'another_viable_option_but_a_silly_idea':
header( "Location: another.php" );
exit;
break;
}
?>
JonnyH
December 8th, 2007, 03:14
Or if you want to do something a bit better and dynamic you could use:
<?php
/**
* We start switching a get variable, to find out what content to display
**/
switch( @$_GET['page'] )
{
//Gets anything you tell it to
default:
header( "Location: ".$_GET['page'].".php" );
exit;
break;
}
?>
Sammie
December 8th, 2007, 03:21
Jonny am i right in saying that one just redirects the page to your location you want, ie your index page is changed to the about page? if so thats not what i am wanting.
JonnyH
December 8th, 2007, 03:25
I just took your latest post as an example. To do more content wise, you could do:
That will include any file, so you can make say, about.php and it'll get that content with ?page=about
<?php
/**
* We start switching a get variable, to find out what content to display
**/
switch( @$_GET['page'] )
{
//Gets anything you tell it to
default:
include $_GET['page'].".php";
exit;
break;
}
?>
Sammie
December 8th, 2007, 04:59
As you will see on my preview the last code you posted Jonny only includes the about page on my index page, which isn't what i want to do.
http://sam.kwix.info/test/
Basically all i want on my index page is (i.e. this is my home page) and on the about.php page i will have (i.e. this is sammie), but the url to the about page will be index.php?page=about, but in saying that i don't want the about page on the index page.
JohnN
December 8th, 2007, 06:20
nononono don't, simply include the get, you're vulnerable to code remote file execution. set up a list of predefined allowed ones.
<?php
$array = array("home"=>"home.php","about"=>"about.php");
if(isset($array[$_GET['page'])){
include_once('path/too/file/'.$array[$_GET['page']]);
}else{
include_once('path/too/file/index.php');
}
?>
Sammie
December 8th, 2007, 06:37
nononono don't, simply include the get, you're vulnerable to code remote file execution. set up a list of predefined allowed ones.
<?php
$array = array("home"=>"home.php","about"=>"about.php");
if(isset($array[$_GET['page'])){
include_once('path/too/file/'.$array[$_GET['page']]);
}else{
include_once('path/too/file/index.php');
}
?>
I just tried your code and when opening the index page i received the following error.
Parse error: syntax error, unexpected ')', expecting ']' in /home/sammie/public_html/test/index.php on line 5
JohnN
December 8th, 2007, 10:27
sorry my bad
<?php
$array = array("home"=>"home.php","about"=>"about.php");
if(isset($array[$_GET['page']])){
include_once('path/too/file/'.$array[$_GET['page']]);
}else{
include_once('path/too/file/index.php');
}
?>
Sammie
December 8th, 2007, 18:05
That code doesn't seem to be working either, my code is.
<?php
$array = array("dp"=>"new-dp.gif","about"=>"about.php");
if(isset($array[$_GET['page']])){
include_once('path/too/file/'.$array[$_GET['page']]);
}else{
include_once('path/too/file/about.php');
}
?>
and i am getting the following error.
Warning: include_once(path/too/file/about.php) [function.include-once]: failed to open stream: No such file or directory in /home/sammie/public_html/test/index.php on line 11
Warning: include_once() [function.include]: Failed opening 'path/too/file/about.php' for inclusion (include_path='.:/usr/lib/php:/usr/local/lib/php') in /home/sammie/public_html/test/index.php on line 11
Sammie
December 8th, 2007, 18:11
That code doesn't seem to be working either, my code is.
<?php
$array = array("dp"=>"new-dp.gif","about"=>"about.php");
if(isset($array[$_GET['page']])){
include_once('path/too/file/'.$array[$_GET['page']]);
}else{
include_once('path/too/file/about.php');
}
?>
and i am getting the following error.
Warning: include_once(path/too/file/about.php) [function.include-once]: failed to open stream: No such file or directory in /home/sammie/public_html/test/index.php on line 11
Warning: include_once() [function.include]: Failed opening 'path/too/file/about.php' for inclusion (include_path='.:/usr/lib/php:/usr/local/lib/php') in /home/sammie/public_html/test/index.php on line 11
----------
Should 'path/too/file/' be changed to which directory my file is in? if so i tryed doing that but the about page text is still including itself on the index page.
JohnN
December 8th, 2007, 18:13
path/too/file i a demo, replace it with whatever the real one is.
Sammie
December 8th, 2007, 18:27
Ok thats what i was thinking, however when changing that the index page has the about page text on it, why is that? or cant that be changed?
My code is
<?php
$array = array("dp"=>"new-dp.gif","about"=>"about.php");
if(isset($array[$_GET['page']])){
include_once('news'.$array[$_GET['page']]);
}else{
include_once('news/about.php');
}
?>
But when trying to use index.php?page=about i get the following error.
Warning: include_once(newsabout.php) [function.include-once]: failed to open stream: No such file or directory in /home/sammie/public_html/test/index.php on line 7
Warning: include_once() [function.include]: Failed opening 'newsabout.php' for inclusion (include_path='.:/usr/lib/php:/usr/local/lib/php') in /home/sammie/public_html/test/index.php on line 7
Preview
http://sam.kwix.info/test/
JohnN
December 8th, 2007, 18:47
http://sam.kwix.info/test/news/index.php does not exist, you'll need us to provide you with the files you want to use.
Sammie
December 8th, 2007, 19:52
I've just moved the index.php file into the news directory so that will come up, what now
JohnN
December 9th, 2007, 08:21
put new-dp.gif and about.php in the news directory, then use this code
<?php
$array = array("dp"=>"new-dp.gif","about"=>"about.php");
if(isset($array[$_GET['page']])){
include_once('news/'.$array[$_GET['page']]);
}else{
include_once('news/about.php');
}
?>
BananaMaster
December 9th, 2007, 11:01
Well I thought I might give this a try so here's a code I just put together. It should work I tested it on my PC.
Here's the code
<?
$page = $_GET['page'];
if(isset($page))
{
ini_set("display_errors","off");
$directory = "include/"; //The directory where the files will be strored to get , eg. if this file was in a directory called news and the files you want to go are in news/include you will put include inside this variable but if it's in the same directory leave it blank.
$fileget = $directory.$page.".php"; //Structure of directory, doesn't need to be edited
$thefile = file_get_contents($fileget) or die("The specified file was not found"); //Gets the contents
echo $thefile; //Shows what was found in that file
}
else
include"main.php"; //If nothing was specified in the ?page= then it will include the main.php file
?>
Sammie
December 9th, 2007, 19:15
JohnN Indeed all them files are in the correct directory and i updated the code with the one you provided me with and i am still getting the following error.:cry2:
Warning: include_once(news/about.php) [function.include-once]: failed to open stream: No such file or directory in /home/sammie/public_html/test/news/index.php on line 11
Warning: include_once() [function.include]: Failed opening 'news/about.php' for inclusion (include_path='.:/usr/lib/php:/usr/local/lib/php') in /home/sammie/public_html/test/news/index.php on line 11
http://sam.kwix.info/test/news/
nwhstaff Cheers buddy, but i am getting an error when loading the page, the code is as following.
<?
$page = $_GET['page'];
if(isset($page))
{
ini_set("display_errors","off");
$directory = "testing99"; //The directory where the files will be strored to get , eg. if this file was in a directory called news and the files you want to go are in news/include you will put include inside this variable but if it's in the same directory leave it blank.
$fileget = $directory.$page."about.php"; //Structure of directory, doesn't need to be edited
$thefile = file_get_contents($fileget) or die("The specified file was not found"); //Gets the contents
echo $thefile; //Shows what was found in that file
}
else
include"index.php"; //If nothing was specified in the ?page= then it will include the index.php file
?>
When on the index page of the directory i get the following error message.
Fatal error: Out of memory (allocated 22544384) (tried to allocate 4864 bytes) in /home/sammie/public_html/testing99/index.php on line 14
and when trying index.php?page=about i am also getting this message.
The specified file was not found
http://sam.kwix.info/testing99/
But if i can get you to work out why the errors are coming up i think that may be what i am needing but i still would like JohnN to try and help.
BananaMaster
December 10th, 2007, 10:32
Ok,
I'll work on it now and i'll edit this post of the new code.
<?
$page = $_GET['page'];
if(isset($page))
{
ini_set("display_errors","off");
$directory = ""; //The directory where the files will be strored to get , eg. if this file was in a directory called news and the files you want to go are in news/include you will put include inside this variable but if it's in the same directory leave it blank.
$fileget = $directory.$page.".php"; //Structure of directory, ONLY EDIT IF FILES ARE NOT .PHP
$thefile = file_get_contents($fileget) or die("The specified file was not found"); //Gets the contents
echo $thefile; //Shows what was found in that file
}
else
{
include"index.php" or die("The was an error in the script on the default page");
}
?>
Ok, There it is. Where it says $fileget = $directory.$page.".php"; you don't need to add anything infront of the .php As that is just the extension but if you was .htm file replace it with .htm
Also the $directory should =""; if it is in the same directory as the script so if it's http://sam.kwix.info/testing99/?page=news and you can access it like http://sam.kwix.info/testing99/news.php then it's blank but if it's http://sam.kwix.info/testing99/files/news.php then you will will put it as $directory = "files";
I hope this fixes it.
Sammie
December 10th, 2007, 16:38
No problems i understand what you mean, so you do not have to add each page into the script? so you could get the files about, news, results etc under the 1 script without adding anything?
I used the same script as you provided but received the following error on the index page for some reason. :devious2:
Warning: include(1) [function.include]: failed to open stream: No such file or directory in /home/sammie/public_html/testing99/index.php on line 15
Warning: include(1) [function.include]: failed to open stream: No such file or directory in /home/sammie/public_html/testing99/index.php on line 15
Warning: include() [function.include]: Failed opening '1' for inclusion (include_path='.:/usr/lib/php:/usr/local/lib/php') in /home/sammie/public_html/testing99/index.php on line 15
JohnN
December 10th, 2007, 17:05
ok. now they're all in the news folder
<?php
$array = array("dp"=>"new-dp.gif","about"=>"about.php");
if(isset($array[$_GET['page']])){
include_once($array[$_GET['page']]);
}else{
include_once('about.php');
}
?>
that will work.
:)
BananaMaster
December 11th, 2007, 12:37
No problems i understand what you mean, so you do not have to add each page into the script? so you could get the files about, news, results etc under the 1 script without adding anything?
I used the same script as you provided but received the following error on the index page for some reason. :devious2:
Warning: include(1) [function.include]: failed to open stream: No such file or directory in /home/sammie/public_html/testing99/index.php on line 15
Warning: include(1) [function.include]: failed to open stream: No such file or directory in /home/sammie/public_html/testing99/index.php on line 15
Warning: include() [function.include]: Failed opening '1' for inclusion (include_path='.:/usr/lib/php:/usr/local/lib/php') in /home/sammie/public_html/testing99/index.php on line 15
Ok to remove that error:
find this line:
include"index.php" or die("The was an error in the script on the default page");
change it to
include"index.php";
Sammie
December 11th, 2007, 14:57
JohnN That code is working, however when going to ?page=about is there any way of removing the index text from that page? or would i be required to set up a blank page and have the script on?
Also what do i do withe the following link of code? what can i change the about.php to?
include_once('about.php');
nwhstaff
Ermm i've just done what you said and i am getting a Fatal error
Fatal error: Out of memory (allocated 20709376) (tried to allocate 18 bytes) in /home/sammie/public_html/testing99/index.php on line 9
<?
$page = $_GET['page'];
if(isset($page))
{
ini_set("display_errors","off");
$directory = ""; //The directory where the files will be strored to get , eg. if this file was in a directory called news and the files you want to go are in news/include you will put include inside this variable but if it's in the same directory leave it blank.
$fileget = $directory.$page.".php"; //Structure of directory, ONLY EDIT IF FILES ARE NOT .PHP
$thefile = file_get_contents($fileget) or die("The specified file was not found"); //Gets the contents
echo $thefile; //Shows what was found in that file
}
else
{
include"index.php";
}
?>
JohnN
December 11th, 2007, 16:11
<?
$page = $_GET['page'];
if(isset($page))
{
ini_set("display_errors","off");
$directory = ""; //The directory where the files will be strored to get , eg. if this file was in a directory called news and the files you want to go are in news/include you will put include inside this variable but if it's in the same directory leave it blank.
$fileget = $directory.$page.".php"; //Structure of directory, ONLY EDIT IF FILES ARE NOT .PHP
$thefile = file_get_contents($fileget) or die("The specified file was not found"); //Gets the contents
echo $thefile; //Shows what was found in that file
}
else
{
include"index.php";
}
?>
^^ your trying to include index.php from index.php and thus causing an infinite loop.
sammie:
<?php
$array = array("dp"=>"new-dp.gif","about"=>"about.php");
if(isset($array[$_GET['page']])){
include_once($array[$_GET['page']]);
}else{
include_once('index_body.php');
}
?>
have ONLY that code in index.php, then make a page called index_body.php and put the content you want to appear by default in there.
hope this helps everyone.
JohnN
December 11th, 2007, 16:12
to use nwhstaff's code instead do this:
<?
$page = $_GET['page'];
if(isset($page))
{
$directory = ""; //The directory where the files will be strored to get , eg. if this file was in a directory called news and the files you want to go are in news/include you will put include inside this variable but if it's in the same directory leave it blank.
$fileget = $directory.$page.".php"; //Structure of directory, ONLY EDIT IF FILES ARE NOT .PHP
$thefile = file_get_contents($fileget) or die("The specified file was not found"); //Gets the contents
echo $thefile; //Shows what was found in that file
}
else
{
?>
DEFAULT CONTENT GOES HERE!
<?php
}
?>
Sammie
December 11th, 2007, 22:40
Cheers JohnN, now i understand the coding more, i think i'll be using the coding you have provided as it's more suitable for what i am doing, ie i don't have all files in the one directory but will keep nwhstaff's code also and use it one day. :)
Powered by vBulletin® Version 4.1.7 Copyright © 2013 vBulletin Solutions, Inc. All rights reserved.