Forum Moderators: coopster

Message Too Old, No Replies

How to Use Variables in a URL to Import Different TXT Files?

Different txt files need to appear on a page based on a variable in the URL

         

clearpixel

2:42 am on May 13, 2006 (gmt 0)

10+ Year Member



I am relatively new and inexperienced with PHP but I have recently learned how to control a page title and pieces of text within a web page using variables in the URL (along with the appropriate scripting in the code for the page). I can also import a remote txt file and have it display on the page (by using <?php
readfile("http://domain.com/file.txt");?> but am trying to figure out how to also control which file is remotely called based on another variable in the URL. My main point in all of this is to be able to generate a highly customized landing page for PPC campaigns by using a special URL for each ad that dovetails with "hot spots" on the landing page itself. Any ideas? Thanks in advance.

eelixduppy

3:31 am on May 13, 2006 (gmt 0)



$text = @file_get_contents("$_GET['file_name']") or die("File not found!");

This way is ok, however it can create some unwanted security issues.

eelix

clearpixel

4:26 am on May 13, 2006 (gmt 0)

10+ Year Member



Thanks for the fast response, but I need a bit more hand-holding:

1. How do I reference (call?) this line of code in the URL?

2. How do I add this code to the page? Just like this:

<?php
$text = @file_get_contents("$_GET['file_name']") or die("File not found!");
?>

3. Also do I swap 'file_name' for the path to where the file is?

eelixduppy

4:33 am on May 13, 2006 (gmt 0)



>>>1. How do I reference (call?) this line of code in the URL?
[yoursite.com...]

>>>2. How do I add this code to the page? Just like this:

Just like this:
<?php
$text = @file_get_contents("http://yoursite.com/$_GET['file_name']") or die("File not found!"); //i added this so that it comes from your server and not someone elses
?>

>>>3. Also do I swap 'file_name' for the path to where the file is?

refer to question 1. You would replace 'test.txt' with the file you want to include.

eelix

clearpixel

6:04 pm on May 13, 2006 (gmt 0)

10+ Year Member



I am probably doing something wrong, but I keep getting this error: Parse error: parse error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in (the path to my file with a note about which line of code the error was on).

Also, just curious about the '$text' portion of the code on the page - do I need to reference it in the URL and how would I proceed if I wanted to import more than one file?

Thanks so much!

eelixduppy

7:01 pm on May 13, 2006 (gmt 0)



the error means that you have a random whitespace where it doesn't belong. (ie $_POST[' fish']). check your code at the line mentioned to see if there is extra whitespace. if you cannot find any, post that one line of code.

To use multiple gets in the url, you do the following:

[yoursite.com?var1=monkey&var2=fish&var3=cheese...]

//the following prints all $_GET variables
print_r($_GET);

eelix

clearpixel

3:39 pm on May 14, 2006 (gmt 0)

10+ Year Member



I couldn't find any spaces in the code. Here is exactly how it looks on my page:

<?php
$text = @file_get_contents("http://dev.example.com/test$_GET['file_name']") or die("File not found!")
?>

The test page I am using is located at <snip> and the text file itself is located at <snip> - I think partly I am confused about how to write the code and how to add the variable phrase to the URL...especially if the URL already contains other variables (as in the case below). Also, if I wanted to have a few different text files (or image files), how would the code change for each on the page and how would the variables for them differ in the URL?

I also previously tested how to pass two variables and change the Title Tag in the URL while also importing a fixed filed into the page (not controllable via the URL). You can see a sample of that page (with all the variables in the URL) here: [dev.example.com...]

[edited by: jatar_k at 3:26 pm (utc) on May 15, 2006]
[edit reason] no urls thanks [/edit]

eelixduppy

3:47 pm on May 14, 2006 (gmt 0)



Change this:

<?php
$text = @file_get_contents("http://yoursite.com/test$_GET['file_name']") or die("File not found!")
?>

to

<?php
$text = @file_get_contents("http://yoursite.com/test/".$_GET['file_name']) or die("File not found!");
?>

To see if that changes your whitespace error. Make sure you have a semi-colon at the end of this line

To use multiple variables you would do something like this:
[yousite.com...]

Good luck!
BTW...no personal links on the forum.

eelix

bartainer

12:29 am on May 15, 2006 (gmt 0)

10+ Year Member



I have tried the above and I am getting "File not found". I have uploaded the file, however it is stil not found.

I am trying to insert text (a table) from a file into a page (without the images). Like using the GET function for an image. Instead of using multiple pages with the GET function I can use one page and the image is placed into that page. I want the same option (effect).

I guess I'm trying to insert a table only that is located on another page I wil call table.php. I want the contents form table.php inserted into table2.php. Table2.php will have all the images etc.

Is this all possible?

Thanks.

eelixduppy

12:37 am on May 15, 2006 (gmt 0)



>>>I want the contents form table.php inserted into table2.php. Table2.php will have all the images etc.

I'm not quite sure what you mean by this. If you only want to add the contents from table.php into table2.php, then you have to use the include() [us3.php.net] function. If this isn't the case, then can you please clearly specify your needs in other words.

eelix

clearpixel

7:16 am on May 15, 2006 (gmt 0)

10+ Year Member



Thanks for your continued help, eelixduppy. I was having a similar problem with "File not Found" displaying on the page and/or nothing showing up at all. I fiddled with the code and, despite my PHP neophyte status, I finally got it to work. Here's how...let me know if I have inadvertently created other possible problems and/or security issues:

Code on the Page:

<?php
$text1 = @file_get_contents("http://www.mysite.com/mydirectory/".$_GET['text1']) or die("File not found!");
print "$text1";
?>

Note 1: I added the last line (print "$text1";) and that made a huge difference.

Note 2: In the original code you provided you had $_GET['file_name'] but I changed 'file_name' to 'text1' - and made sure that it was used in the other two spots in the code (basically all of those elements were the same).

Note 3: I tried a version where I called two different text files on the same page, but used 'text2' for the second call and it worked! :)

URL to Access the Page:

[mysite.com...]

eelixduppy

11:10 am on May 15, 2006 (gmt 0)



I'm sorry. I kept them both the same(get variable and url variable) for my second post, but then i don't know what i was thinking. I'm glad you got it though.

eelix