Shoutbox

PHP Question - Printable Version

-Shoutbox (https://shoutbox.menthix.net)
+-- Forum: MsgHelp Archive (/forumdisplay.php?fid=58)
+--- Forum: Skype & Technology (/forumdisplay.php?fid=9)
+---- Forum: Tech Talk (/forumdisplay.php?fid=17)
+----- Thread: PHP Question (/showthread.php?tid=90002)

PHP Question by Eddie on 03-31-2009 at 02:29 PM

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.


RE: PHP Question by matty on 03-31-2009 at 03:42 PM

You mean like a query string?

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

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


RE: PHP Question by stoshrocket on 03-31-2009 at 03:48 PM

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 :)
RE: PHP Question by Eddie on 03-31-2009 at 04:28 PM

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.
RE: PHP Question by MeEtc on 03-31-2009 at 04:43 PM

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


RE: PHP Question by Eddie on 03-31-2009 at 04:49 PM

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.


RE: PHP Question by stoshrocket on 03-31-2009 at 05:07 PM

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.
RE: PHP Question by Eddie on 03-31-2009 at 05:16 PM

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.


RE: PHP Question by stoshrocket on 03-31-2009 at 05:30 PM

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 =)
RE: PHP Question by Eddie on 03-31-2009 at 05:47 PM

Now THATS what im after, thank you very much :P You've been of great help :D

+1 (Y)


RE: PHP Question by stoshrocket on 03-31-2009 at 05:49 PM

quote:
Originally posted by Eddie
Now THATS what im after, thank you very much :P You've been of great help :D

+1 (Y)
You're very welcome, I like teh PHP =)
RE: PHP Question by davidpolitis on 04-02-2009 at 07:43 AM

You might wanna also use a .htaccess file to disallow access to all the files in $dir.


RE: PHP Question by Eddie on 04-02-2009 at 08:33 AM

Another question, how would i go about including different directories, i understand what you said, but then your llama output only allows for one directory.

For example i have the following directories.
/page/info/
/page/users/
/page/resources/

Using your method above how would i do this? If its not possible i will just combine all the pages, but yeah.

Also further questions, if anyone knows PHP/MySQL information...

What i'm making is a Virtual Airline for a friend of mine and myself, i am very bored at the moment incase you wish to know, but anyway...im creating a routes table on my database and what i want to happen is;

On my routes page the url will look like http://www.address.com/index.php?page=routes

And of course on that page there will be a table listing all the routes, that are to be grabbed from my database and sorted by "FlightID". For example i have a flight AAA123 in the table, i want the FlightID to be a link to a page like the following;

http://www.address.com/index.php?page=routes&flightid=AAA123

And i want this to occur to every row for each FlightID.

(On a note to the above, it would be even better, if that link would load up a generic page but with the correct FlightID in the url, which it loading all of the data from the AAA123 row in my database.)

Next up is a submit report page, im having a little trouble with it, ive at the moment got it working on seperate pages for each FlightID, which is quite an effort, but isnt really very convenient if i wished to change them.

What will happen is, on the page;
http://www.address.com/index.php?page=booked_flights&flightid=AAA123

There will be a link to File a Pirep. Is it possible to make that link open a new generic page, which will automatically fill in data from the AAA123 row of my database as part of a form, but also allowing user-input, if you don't understand i will upload an example to my website so you can see what i mean.

Sorry if it doesnt make sense, im trying to explain the best i can what i need, it has been a while since i coded like this so im just having a bit of trouble getting it "on to paper".


RE: PHP Question by stoshrocket on 04-02-2009 at 12:10 PM

quote:
Originally posted by Eddie
Another question, how would i go about including different directories, i understand what you said, but then your llama output only allows for one directory.

For example i have the following directories.
/page/info/
/page/users/
/page/resources/

Using your method above how would i do this? If its not possible i will just combine all the pages, but yeah.

Easy fix, you just add the directory before the page name variable. So for your exmaple detailed in your post...
PHP code:
$page = $_GET['p'];
$llama = "./page/"."$page".".hz";
 
if(file_exists($llama) == TRUE){
include ($llama);
}
else{
include("error.php")
}


then a link to file.hz in ./page/info/ would be...
HTML code:
<a href="?page=info/file">link</a>


Edit: Posted re-written/heavily edited re to segosa's comment about the coding, and to rectify a couple of minor mistakes.
RE: RE: PHP Question by davidpolitis on 04-02-2009 at 12:52 PM

quote:
Originally posted by Eddie
On my routes page the url will look like http://www.address.com/index.php?page=routes

And of course on that page there will be a table listing all the routes, that are to be grabbed from my database and sorted by "FlightID".
Use a MySQL query something along the lines of this:
code:
SELECT FlightID, FlightName, FlightDate FROM news ORDER BY FlightID ASC

quote:
Originally posted by Eddit
For example i have a flight AAA123 in the table, i want the FlightID to be a link to a page like the following;

http://www.address.com/index.php?page=routes&flightid=AAA123
Again, use a query something along the lines of the query below for the page to select your Flight:
code:
SELECT * FROM FlightTable WHERE FlightID=$FlightID

If you want, add me on Messenger (check my profile) and I'll try help you with your pages and try fix code for you when I can. :P
RE: PHP Question by segosa on 04-02-2009 at 10:05 PM

PHP code:
if(file_exists($llama) == TRUE){
include ($llama);
}
elseif(file_exists($llama) == FALSE){
include("error.php")
}


You seem to know what you're doing, so why do you write code like this? Assuming no caching, not only does it require two (if the first test fails) filesystem queries, but it's just downright pointless to do the check twice. If the first fails, then the result of file_exists is obviously false, and hence the else clause would be more than suitable.

PHP code:
if(file_exists($llama) == TRUE){
include ($llama);
}
else {
include("error.php")
}


quote:
Another way of doing it so you don't end up filling the url with messy slashes (and possibly confuddling your browser)

Why would it confuse the browser? They are generally indifferent to everything placed after the domain name, and will automatically urlencode what is necessary (like spaces).

http://blah.com/?f'dfd//s?'sd/744$///////idi2?444

will still result in "GET /?f'dfd//s?'sd/744$///////idi2?444 HTTP/1.1" being sent to the server (possibly after urlencoding). Using another character just adds another pointless level of abstraction.
RE: PHP Question by stoshrocket on 04-02-2009 at 11:23 PM

quote:
Originally posted by segosa

You seem to know what you're doing, so why do you write code like this? ...

All true, I don't know why I did that, probably down to a combination of tiredness during typing and just inefficient coding... Sorry and thanks for pointing it out though.

Edit: Post in question edited in response
RE: RE: PHP Question by davidpolitis on 04-03-2009 at 06:49 AM

quote:
Originally posted by segosa
PHP code:
if(file_exists($llama) == TRUE){
include ($llama);
}
else {
include("error.php")
}


quote:
The == TRUE part also may as well as be removed as file_exists returns a boolean value...
PHP code:
if(file_exists($llama)){
include ($llama);
}
else {
include("error.php")
}

:P


RE: PHP Question by Eddie on 04-03-2009 at 06:50 AM

code:
<div id="content">
    <?
      $page = GET['p'];
      $llama = "./page/"."$page".".mdu";
      if(file_exists($llama) == TRUE){
          include ($llama);
      }
      else{
          include("error.php")
      }
?>
    </div>

Is returning the following error;

Parse error: parse error in C:\xampp\htdocs\vabase\main.php on line 29

Line 29 is "$page = GET['p'];"
RE: PHP Question by NanaFreak on 04-03-2009 at 06:54 AM

do you have any previous php code? because you could have an error there... also i see that there is no ; on the include("error.php") ;)


edit: you need to change the line that is erroring to $_GET['p']; ;)

also to highlight a line in code you can use

>>> the text you are wanting to highlight <<<

in the code tags


RE: RE: PHP Question by davidpolitis on 04-03-2009 at 06:55 AM

quote:
Originally posted by Eddie
code:
<div id="content">
    <?
      $page = GET['p'];
      $llama = "./page/"."$page".".mdu";
      if(file_exists($llama) == TRUE){
          include ($llama);
      }
      else{
          include("error.php")
      }
?>
    </div>

Is returning the following error;

Parse error: parse error in C:\xampp\htdocs\vabase\main.php on line 29

Line 29 is "$page = GET['p'];"
PHP code:
<div id="content">
<?php
 $page = $_GET['p'];
 $llama = "./page/".$page.".mdu";
 if(file_exists($llama)) {
  include($llama);
 }
 else {
  include("error.php");
 }
?>
</div>


I"ll explain what was wrong with your code.
1. I changed the PHP opening to "<?php" because short tags (<?) need to be enabled on whatever server you decide to upload to.
2. Line 3 was using "GET", instead of "$_GET".
3. Where you set $llama, you had $page in quotes, unnecessarily.
4. Line 5 is using an if with file_exists. Since file_exists returns a boolean, "if(file_exists($llama))" works and is also shorter.
5. The include in your else contained no ";" (thanks for the heads up nana. Didn't notice it. XD)

Eddie, you should try find some books at the library like I did when I first started or find some e-books. You need to learn the language at least a bit before trying to make yourself some sought of system using database entries etc.

P.S. Sorry for editing my post so many times. XD
RE: PHP Question by NanaFreak on 04-03-2009 at 07:06 AM

quote:
Originally posted by davidpolitis
I changed the PHP opening to "<?php" because short tags (<?) need to be enabled on whatever server you decide to upload to.
this is not always the case, my host (hostgator) has short tags enabled by default, and really all hosts should (unless its a free host or something)
RE: RE: PHP Question by davidpolitis on 04-03-2009 at 07:16 AM

quote:
Originally posted by NanaFreak
quote:
Originally posted by davidpolitis
I changed the PHP opening to "<?php" because short tags (<?) need to be enabled on whatever server you decide to upload to.
this is not always the case, my host (hostgator) has short tags enabled by default, and really all hosts should (unless its a free host or something)
While I do realise WAMPSERVER is not used for actual servers, it's default php.ini has short open tags disabled and says this:
quote:
Using short tags is discouraged when developing code meant for redistribution since short tags may not be supported on the target server.
Meh... it doesn't really matter. Everyone has their own preferences. E.g. usually I put all of my braces on new lines.
RE: RE: RE: PHP Question by segosa on 04-03-2009 at 08:09 AM

quote:
Originally posted by davidpolitis
quote:
Originally posted by segosa
PHP code:
if(file_exists($llama) == TRUE){
include ($llama);
}
else {
include("error.php")
}


The == TRUE part also may as well as be removed as file_exists returns a boolean value...
PHP code:
if(file_exists($llama)){
include ($llama);
}
else {
include("error.php")
}

:P

I would consider the explicit comparison to TRUE an issue of style, which is why I didn't complain about it.
RE: PHP Question by Felu on 04-03-2009 at 09:37 AM

quote:
Originally posted by davidpolitis
quote:
Originally posted by segosa
PHP code:
if(file_exists($llama) == TRUE){
include ($llama);
}
else {
include("error.php")
}


The == TRUE part also may as well as be removed as file_exists returns a boolean value...
PHP code:
if(file_exists($llama)){
include ($llama);
}
else {
include("error.php")
}

:P
If you really care about that... Then you're just executing 1 line of code for the condition... you don't even need the curly brackets in that case :P

PHP code:
if(file_exists($llama))
    include ($llama);
else
    include("error.php");


RE: RE: PHP Question by davidpolitis on 04-03-2009 at 10:15 AM

quote:
Originally posted by Felu

PHP code:
if(file_exists($llama))
    include ($llama);
else
    include("error.php");


You forgot to remove the space after the first occurance of "include". :P

Anyway, using require instead of include may be something to think about...

quote:
require() is identical to include() except upon failure it will produce a fatal E_ERROR level error. In other words, it will halt the script whereas include() only emits a warning (E_WARNING) which allows the script to continue.