Web Hosting Talk







View Full Version : PHP, variables to set content


RS Shamil
05-24-2009, 11:04 AM
Hello, I am using variables to set content, however, due to a large variety of pages, I can't keep adding all of them to the script. Is there anyway that I can declared them to be in the variable?

I am currently using:

<?php
//If is defined URL variable 'aboutme'
if(isset($_GET['app=aboutme'])){
// include page about me
include('pages/iaboutme.php');
//else if is defined URL variable 'interests'
}else if(isset($_GET['app=interests'])){
// include page interests
include('pages/in-interest.php');
// in all other cases include the home page
} else {
include('pages/home.php');
}
?>

So: index.php?app=aboutme, gives the about me page, in the div.

Most of the files, are in the application folder, however, some are in other folders. Is there anyway in which I could make the URL such as: index.php?app=pages&aboutme, so that, it first goes to the folder, pages, then includes the said file, aboutme ?

I hope that's clear enough for what I am intending to do.

Thanks
RadonSys Shamil :)

oldunis
05-24-2009, 12:09 PM
I would suggess that you use two different variables, one for the folder and one for the file. If the folder is empty, use a default value!

But for security concern, I would suggess putting all the files to include into one folder.


<?php

/*
To include the file aboutme.php:
Usage: index.php?app=aboutme
To include the file interests wich is in the folder private
Usage: index.php?app=interests&f=private
*/

if(empty($_GET['f']))
$folder = "pages/"; //Default folder
else
$folder = str_replace("/", "", $_GET['f']) . "/";

if(empty($_GET['app'])) {
include('pages/home.php');
} else {
if(file_exists($folder.$app.".php")) {
include($folder.$app.".php");
} else {
//If someone calls a file which does not exist, we call the home page!
include("page/home.php");
}
}

?>

RS Shamil
05-24-2009, 01:03 PM
Interesting, thanks :), just a question though, how would you "find" a script located in pages/admin ? ie. pages/admin/adduser.php ?

siforek
05-24-2009, 01:53 PM
Interesting, thanks :), just a question though, how would you "find" a script located in pages/admin ? ie. pages/admin/adduser.php ?


If you mean find the page name you could do something like:
$filename = basename($_SERVER['PHP_SELF'], ".php");

RS Shamil
05-24-2009, 02:11 PM
That'll be useful at some point, however, let's say, that I have one of pages links out to :

/pages/admin/adduser.php, do I use: index.php?app=adduser&f=pages/admin ?

cselzer
05-24-2009, 02:13 PM
here...


<?PHP
$enabledPages = array('page1','page2','page3','page4');
$defaultPage = "page1";
$pagesDir = "/var/Pages";

if($_GET['page'] != "")
{
if(in_array($_GET['page'], $enabledPages))
{
if(file_exists($pagesDir . "/" . $_GET['page'] . ".php"))
{
include($pagesDir . "/" . $_GET['page'] . ".php");
}
}
}
else
{
if(file_exists($pagesDir . "/" . $defaultPage . ".php"))
{
include($pagesDir . "/" . $defaultPage . ".php");
}
}


Untested, wrote in here.. but this would be secure...

RS Shamil
05-24-2009, 02:27 PM
Thank you very much :), now I've done that, the next thing is the automatic IP ban system, fun!

Thanks to everyone though, I really appeciate it - since I'm learning some parts of PHP.

oldunis
05-24-2009, 02:28 PM
You are welcome! Have fun with php ;)

RS Shamil
05-24-2009, 08:31 PM
You are welcome! Have fun with php ;)

Just noticed with your script, that you can't use variables i.e

index.php?app=usercp&changepw (or have I formatted this incorrectly?)

?

iEve
05-26-2009, 12:21 PM
You should be able to use any $_GET variables, just make sure you are supplying a $_GET['page'] parameter, otherwise it will resort to the default page.

so it can be index.php?app=usercp&changepw&page={whatever}

You may think about adding another parameter to determine the folder that way you don't have to deal with a '/' in the url.

RS Shamil
05-26-2009, 05:04 PM
You should be able to use any $_GET variables, just make sure you are supplying a $_GET['page'] parameter, otherwise it will resort to the default page.

so it can be index.php?app=usercp&changepw&page={whatever}

You may think about adding another parameter to determine the folder that way you don't have to deal with a '/' in the url.

Sorted it out :)

Ok, I know how to do this, but, let's say that I've a folder located at /admin/pma, and I wanted it's own post tag, pconf, for which there should be a redirection, would I have:

if ($_POST['pconf']) {something which I don't know, becuase, headers have already gone....;}

I'm not too sure :s

alfyles
05-30-2009, 03:45 PM
It's ok to have / in the parameter. I do that a lot :)

If you wanted to do header redirection then you could just put the pconf value into a variable first and then pass it onto the get parameter in the redirection url.

$pconf = $_POST['pconf'];
if ($pconf) {
header('Location: blabla.php?pconf=$pconf');
}

RS Shamil
05-30-2009, 04:46 PM
Thanks :) I definately will need that in another project :D