What happened to the Messenger Plus! forums on msghelp.net?
Shoutbox » MsgHelp Archive » Skype & Technology » Tech Talk » PHP Question

Pages: (3): « First [ 1 ] 2 3 » Last »
PHP Question
Author: Message:
Eddie
Veteran Member
*****


Posts: 2078
Reputation: 30
32 / Male / Flag
Joined: Oct 2005
Status: Away
O.P. PHP Question
Hey folks,

Just a quick question, i havent coded for a while so i cant remember how to do the following in php...

I see websites with pages like index.php?next_page blablabla...
Can anyone either point me to a link or tell me how? thanks.
...there used to be a signature here :)
03-31-2009 02:29 PM
Profile PM Web Find Quote Report
matty
Scripting Guru
*****


Posts: 8336
Reputation: 109
39 / Male / Flag
Joined: Dec 2002
Status: Away
RE: PHP Question
You mean like a query string?

code:
index.php?x=1&y=0

PHP code:
<?php
 
    echo $_GET['x'].'<br />'.$_GET['y'];
 
?>


This post was edited on 03-31-2009 at 03:44 PM by matty.
03-31-2009 03:42 PM
Profile E-Mail PM Find Quote Report
stoshrocket
Senior Member
****

Avatar
formerly methos

Posts: 748
Reputation: 31
33 / Male / Flag
Joined: Aug 2005
RE: PHP Question
That's done something using "$_GET" to create check against a variable...

In php, you have assign variables to the end of the url and then call them back using the global variable $_GET, so to create an easy include function, you would have your links changing that variable, and a block of code to check what that variable is, so for example, if we had the variable "page" and the includes 1.php and 2.php and the variables for those pages being "one" and "two" respectively, we would have the big block of code to check what the variable is:

PHP code:
$page = (isset($_GET['page'])) ? $_GET['page'] : "";
   
if($page=="" or $page=="one"){
    include("1.php");
}
elseif($page=="two"){
    include("2.php");
}


This uses the $_GET and sets the variable $page to the value that has been set in the URL (for example www.bbq.com?page=one would mean the variable page is set to one), then checks what the variable is, and if the variable is one or not set then it includes 1.php and if the variable is two it includes 2.php.

To change this variable (ie, when you click a link) you use the following code:

PHP code:
<a href="?page=two">goto page 2</a>


This is a self-explanatory line of code.

This method can also be used to string variables and can be incredibly useful. To string variables, you just set the variables in the same way. If you need any more help feel free to post :)

This post was edited on 03-31-2009 at 07:40 PM by stoshrocket.
formerly methos
03-31-2009 03:48 PM
Profile PM Web Find Quote Report
Eddie
Veteran Member
*****


Posts: 2078
Reputation: 30
32 / Male / Flag
Joined: Oct 2005
Status: Away
O.P. RE: PHP Question
quote:
Originally posted by methos
That's done something called the "get" function...

In php, you have assign variables to the end of the url and then call them back using the GET function, so to create an easy include function, you would have your links changing that variable, and a block of code to check what that variable is, so for example, if we had the variable "page" and the includes 1.php and 2.php and the variables for those pages being "one" and "two" respectively, we would have the big block of code to check what the variable is:

PHP code:
$page = (isset($_GET['page'])) ? $_GET['page'] : "";
   
if($page=="" or $page=="one"){
    include("1.php");
}
elseif($page=="two"){
    include("2.php");
}


This uses the GET function and sets the variable $page to the value that has been set in the URL (for example www.bbq.com?page=one would mean the variable page is set to one), then checks what the variable is, and if the variable is one or not set then it includes 1.php and if the variable is two it includes 2.php.

To change this variable (ie, when you click a link) you use the following code:

PHP code:
<a href="?page=two">goto page 2</a>


This is a self-explanatory line of code.

This method can also be used to string variables and can be incredibly useful. To string variables, you just set the variables in the same way. If you need any more help feel free to post :)
That method is relatively similair to what i've used before, but before, all you had to do was list the different page names, link to the directory the files were in and it would then present them files in a similair fashion to how you did it, hope you understand.
...there used to be a signature here :)
03-31-2009 04:28 PM
Profile PM Web Find Quote Report
MeEtc
Patchou's look-alike
*****

Avatar
In the Shadow Gallery once again

Posts: 2200
Reputation: 60
38 / Male / Flag
Joined: Nov 2004
Status: Away
RE: PHP Question
Be careful though when you do that, because malicious users can use this to load arbituary code. bbq.com?page=./../passwords.txt would allow a user to load a file that normally wouldn't be able to load, or even bbg.com?page=http://malicioussite.com/badpage.php

Use a regex to filter these out, make sure the file exists, or even better us an associative array. bbq.com?page=1 loads one.php as an example
[Image: signature/]     [Image: sharing.png]
I cannot hear you. There is a banana in my ear.
03-31-2009 04:43 PM
Profile PM Web Find Quote Report
Eddie
Veteran Member
*****


Posts: 2078
Reputation: 30
32 / Male / Flag
Joined: Oct 2005
Status: Away
O.P. RE: PHP Question
I dont think you guys are quite following me with what i want now :S

I want a main index.php page. Where the page itself remains exactly the same and only the "main" area of a page, that is selected is changed with the contents of another .php file or .html file etc.

Sort of like an iframe i suppose, but in PHP.

Hope you understand.
...there used to be a signature here :)
03-31-2009 04:49 PM
Profile PM Web Find Quote Report
stoshrocket
Senior Member
****

Avatar
formerly methos

Posts: 748
Reputation: 31
33 / Male / Flag
Joined: Aug 2005
RE: PHP Question
quote:
Originally posted by Eddie
That method is relatively similair to what i've used before, but before, all you had to do was list the different page names, link to the directory the files were in and it would then present them files in a similair fashion to how you did it, hope you understand.
Yes this can be done, by simply saying
code:
<a href="?page=include.php">linkage</a>

and then from this, you just put this where you want to include it which would be
code:
$llama = $_GET['page'];

include ($llama);


However, as MeEtc was saying this should not be done because...
quote:
Originally posted by MeEtc
malicious users can use this to load arbituary code. bbq.com?page=./../passwords.txt would allow a user to load a file that normally wouldn't be able to load, or even bbg.com?page=http://malicioussite.com/badpage.php

Therefore, it would be safer to use the method entailed in my first post and have variables associated to each page; it's more secure, and better practice.
formerly methos
03-31-2009 05:07 PM
Profile PM Web Find Quote Report
Eddie
Veteran Member
*****


Posts: 2078
Reputation: 30
32 / Male / Flag
Joined: Oct 2005
Status: Away
O.P. RE: PHP Question
Yeah the method i used to use followed the logic from your first page, but instead of having seperate variable things like you have, you had all your page names.

So for example, we used to use a random format, for our old site we used .hz. So in the php code we would have it realising the format name, posting it like any other include but only reading the page names from 'test', 'test1', 'test2' which it would realise are test.hz, test1.hz and test2.hz, and it would show as index.php?p=test or index.php?p=test1

Your original post does this, but in a way that is slightly different and not completely how i wanted.
...there used to be a signature here :)
03-31-2009 05:16 PM
Profile PM Web Find Quote Report
stoshrocket
Senior Member
****

Avatar
formerly methos

Posts: 748
Reputation: 31
33 / Male / Flag
Joined: Aug 2005
RE: PHP Question
quote:
Originally posted by Eddie
Yeah the method i used to use followed the logic from your first page, but instead of having seperate variable things like you have, you had all your page names.

So for example, we used to use a random format, for our old site we used .hz. So in the php code we would have it realising the format name, posting it like any other include but only reading the page names from 'test', 'test1', 'test2' which it would realise are test.hz, test1.hz and test2.hz, and it would show as index.php?p=test or index.php?p=test1

Your original post does this, but in a way that is slightly different and not completely how i wanted.
Ah! Very interesting, although it's not 100% secure it would stop a lot of potential malicious users... A simple way to do this would be to get the page variable, then add the extension on and include this, which in code form would be:

PHP code:
$extension = ".hz";
$llama = "$_GET['page']"."$extension";
 
include ($llama);


Line by line, this defines your extension, then retreives your page variable and attaches the extension to the end and includes the page that this is called. You could probably do with checking the file exists using file_exists to prevent any messy error pages, which could easily be done by shoving it into an if statement and having the error include in the else bracket:

PHP code:
$extension = ".hz";
$llama = "$_GET['page']"."$extension";
 
if(file_exists($llama) == TRUE){
include ($llama);
}
elseif(file_exists($llama) == FALSE){
include("error.php")
}


This would mean a link to include page1.hz would look like this btw:
HTML code:
<a href="?page=page1">page one</a>


NB:Edited link, it stated it should be "page1" when it should be "?page=page1", sorry!

SIDE NOTE:
To increase security, might be worth incresing the size of the random extension and throwing a few numbers in there, this way it can't be stumbled upon as easily =)

FURTHERMORE:

If you wanted to keep all of your includes in a seperate folder or altogether seperate directory, you could assign a variable called $dir and then set this to be your include folder directory and attach it to $llama in the same way... ie, it becomes

PHP code:
$dir = "./some/file/dir"; //extra line to define dir
$llama = "$dir"."$GET_['page']"."$extension"; //new $llama line


FURTHERMORE SIDE NOTE:

You could also have different subfolders and a seperate GET variable if you REALLY wanted... So lets take that example. Imagine you had a database of users all stored in nice text files. Each user has their own folder in the dir ./data/users/ so a user with username methos would have a folder directory ./data/users/methos/ with all their user data in. Say we had a profile page that we wanted to include, and it was stored within the users data folder, we'd have two GET variables: user and page, and we could use the following code to find the correct profile:

PHP code:
$extension = ".hz"; //sets the extension variable
 
//this two line section defines the two variables included in the URL, you don't have to do this - you could just substitue the variables in the $llama variable for the values defined here, but this way it shows it nice and clearly and allows for explanation and comments...
$username = $_GET['user'];
$page = $_GET['page'];
 
//the directory that the username folders will be in
$dir = "./data/users/";
 
//defines the path of the file to include in the following format: directory-user-folders-are-in/user-folder-name/page-to-include.extension
$llama = "$dir"."$username"."$page"."$extension";
 
//checks whether this file exists, if it does include it, if it doesnt include the page error.php
if(file_exists($llama) == TRUE){
include ($llama);
}
elseif(file_exists($llama) == FALSE){
include("error.php")
}


Which for the url (or link to the url of) "http://www.bbq.com/index.php?user=methos&page=profile" would have the page index.php and it would include the file ./data/users/methos/profile.hz

Hope all this helps :D

MASS EDIT: I've changed a couple of explanations, corrected some spelling errors and added some php comments to specifically explain coding =)

This post was edited on 03-31-2009 at 06:38 PM by stoshrocket.
formerly methos
03-31-2009 05:30 PM
Profile PM Web Find Quote Report
Eddie
Veteran Member
*****


Posts: 2078
Reputation: 30
32 / Male / Flag
Joined: Oct 2005
Status: Away
O.P. RE: PHP Question
Now THATS what im after, thank you very much :P You've been of great help :D

+1 (Y)
...there used to be a signature here :)
03-31-2009 05:47 PM
Profile PM Web Find Quote Report
Pages: (3): « First [ 1 ] 2 3 » Last »
« Next Oldest Return to Top Next Newest »


Threaded Mode | Linear Mode
View a Printable Version
Send this Thread to a Friend
Subscribe | Add to Favorites
Rate This Thread:

Forum Jump:

Forum Rules:
You cannot post new threads
You cannot post replies
You cannot post attachments
You can edit your posts
HTML is Off
myCode is On
Smilies are On
[img] Code is On