...

View Full Version : useing ?name=blah - Some help



Anthony2oo4
06-27-2005, 01:06 PM
Ok, I am not great with PHP and just started learning not long ago. i was wondering how I can use this type of method and what its called.


index.php?name=something

I have a template and I was wondering if this is possible.

Have a table in my templat that holds the content. But I want the content to load from a php file.

So for example, if the address is index.php?name=shared

Then I want the contents of shared.php to be loaded inside this table. I think it is possibl and I should e able to mang on my own I just need to Know how to use the above statments:


index.php?name=something

Thanks for any time and effort ;)

delinear
06-27-2005, 01:35 PM
If you just want to change the included file, sure, that's straight forward enough. Just do something like:


if(isset($_GET['name']) {
include($_GET['name'] . '.php');
}

Serex
06-27-2005, 01:46 PM
<?
function validate_page($name)
{
preg_match("/(http(s)?:\/\/)?((www\.)?([^\/]+)\.([\w+]{0,4}))(\/(.*))?/", $name, $check);
/* Here you can decide if you want the format in url to exist.
If you do, add it to your $allowed sites var. */
$allowedsites = array("urlone.com", "urltwo.com");
if (!empty($check[3]))
{
// $check comes back as valid, matches something...
if ($allowedsites)
{
if (in_array($check[3], $allowedsites))
{
return true;
}
else
{
return false;
}
}
else
{
// Nothing in allowed sites, though there is a domain found, false it.
return false;
}
}
else
{
return true;
}
}

// Then call it on page:
if (validate_page($_GET['name']))
{
include($_GET['name'] . ".php");
}
else
{
// include the error file
include('error.php');
}
?>


I had a few problems with people remotely running scripts through my URL and it was considered a security risk so my site got shut down. So id consider taking a bit of caution when using something like this. Above is a work around that checks to see if the page your including is located on your domain. Not really a need for regex but why not :p

edit: changed var name in preg_match

Nightfire
06-27-2005, 02:47 PM
Simple way.


$pages = array("index","contents","news","etc");

if(in_array($_GET['name'],$pages)){
include($_GET['name'].'.php');
}else{
include("error.php");
}

Anthony2oo4
06-27-2005, 04:09 PM
Thanks guys for all replies, I couldnt get first on to work. Second on from serex worked fine, but gixes me an error on line 4 all the time:


Notice: Undefined variable: page in c:\program files\easyphp1-8\www\test.php on line 4

Nightfire: your code looks good too, but I dont wanna have to list all the pages :(
Thanks all of you.

SeeIT Solutions
06-27-2005, 04:17 PM
on line 4, change page to name

Anthony2oo4
06-27-2005, 10:28 PM
OK, guys, I think am gonna go with serex's solution ;)

But I have one more proble. SeeIT seemed to fix the above problem, but when no page is called, for example if index.php is just called, it dosn't seem to call error.php :confused:

Does anyone know why?

Also, what is this called, useing the index.php?name=blah

thanks again guys, great job ;)

Serex
06-28-2005, 03:21 AM
if (validate_page($_GET['name']))
{
include($_GET['name'] . ".php");
}
else
{
// include the error file
include('error.php');
}


it checks to see if the page is valid. and if not it includes an error page that you could create. so it would only include the error page if say the file trying to be included was located somewere else i.e www.domain.com?name=www.domain2.com/script

we can add a check to see if the page is set...
What you could also do is add



if (validate_page($_GET['name']))
{
if (isset($name))
{
include($_GET['name'] . ".php");
}
else
{
// this is the main page thats loaded up when the site
// is visited
include("home.php");
}
}
else
{
// error page displayed when a page that you dont allow
// is trying to be included
include("error.php");
}


that checks to see if you have set a value for name. If not it includes the "home.php" page.

hope it helps

Anthony2oo4
06-28-2005, 01:30 PM
Thanks serex, that worked but still gabe me errors, :(

I tried to do my own, lol, and here is what I got.


if ($_GET['name'] == "") {
include ("Main.php");
} else {
if (validate_page($_GET['name'])) {
include($_GET['name'] . ".php");
} else {
include "error.php";
}
}

If i just call index.php then it shows me Main.php but gives this line above it:


Notice: Undefined index: name in c:\program files\easyphp1-8\www\platrip\pages\page.php on line 34
Line 34 =
if ($_GET['name'] == "") {
If I call a page such as index.php?name=hello then it shows me the hello page with no problems. If i call a page that dosnt exsist, then it gives me errors saying that can't find file on line woteva.

Can anyone fix these problems?

Thanks again for your time :) :thumbsup:

SeeIT Solutions
06-28-2005, 01:33 PM
Change the first line to


if (!isset($_GET['name']) || $_GET['name'] == "") {

Anthony2oo4
06-28-2005, 01:56 PM
Yep, that worked SeeIT, thanks :D

Ok, now am gonna go and play around with this, my next task is to make the right blocks disappear if r=n lol.

I might be back ;)

Thanks all :thumbsup:

Anthony2oo4
06-28-2005, 02:10 PM
OK, am back again. Now am trying to make the right blocks disssapear only if r=n, if it dosnt = n and its not even set, then it should just show the blocks. Here is what i have:


<?
//See if right blocks is needed
if (!isset($_GET['r']) || $_GET['r'] == "n") {
echo "<table width=\"0\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">";
} else {
echo "<table width=\"160\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">";
}
?>
<tr>
<?
if (!isset($_GET['r']) || $_GET['r'] == "n") {
echo "";
}else{
include ("includes/right.php");
}
?>

This works without any php errors, but the only problem is, if r is not set, ie it isnt called, then it seems to hide the blocks, if i do index.php?r=f then it shows the blocks. If i do r = n then it hides them, so it works, except for the fact that if its not called, it hides then anyway.

How can I fix this?

Anthony2oo4
06-28-2005, 02:15 PM
Ah, I've just seen that the code had a "!" in it, which I think means, does not or is not, so I took them out on both if statments, and now the blocks show, only I get these errors:


Notice: Undefined index: r in c:\program files\easyphp1-8\www\platrip\index2.php on line 219

Notice: Undefined index: r in c:\program files\easyphp1-8\www\platrip\index2.php on line 227

both lines are the begining of the if statments. If I type r=n then the errors dont show.

Help :(

SeeIT Solutions
06-28-2005, 02:21 PM
Try this


<?
//See if right blocks is needed
if (isset($_GET['r']) && $_GET['r'] == "n") {
echo "<table width=\"0\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">";
} else {
echo "<table width=\"160\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">";
}
?>
<tr>
<?
if (isset($_GET['r']) && $_GET['r'] == "n") {
echo "";
}else{
include ("includes/right.php");
}
?>

Anthony2oo4
06-28-2005, 02:49 PM
Yep, that worked great, THANK YOU !!!!! :thumbsup: :D



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum