Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   PHP (http://www.programmingforums.org/forum29.html)
-   -   Storing passed information into a text file (http://www.programmingforums.org/showthread.php?t=9690)

dunowhodoyou May 7th, 2006 3:39 PM

Storing passed information into a text file
 
Im doing an project and im not the best at php at all so really need your help, I'm trying to send user data to another location and store it to a text file, so i pass the information over and catching it with this code

<?php
$colorpreference = $_GET['userinformation'];
$ip = getenv ('REMOTE_ADDR');
$date=date("j F, Y, g:i a");;
$referer=getenv ('HTTP_REFERER');
$fp = fopen('colorpreference.txt', 'a');
fwrite($fp, 'Data: '.$colorpreference.'<br> IP: ' .$ip. '<br> Date and Time:
' .$date. '<br> Referer: '.$referer.'<br><br><br>');
fclose($fp);
header ("Location: /thankyou.html");
?>


This is the data being passed is

http://www.myserver.com/test2.php?us...olour=#FFFFFF;

but once the script gets to saving the # symbol it stops and all i get
in the text file is

Data: Email=myemail@gmail.com; Username=correctusername;
Colour=<br> IP: correct ip here<br> Date and Time: 4 May,
2006, 4:30 pm<br> Referer: <br><br><br>

Is there anyway i could just tell the script to convert everything to
a string and save it or maybe since i know whats being passed could i
just set it up to expect the colour field to be characters, or take out the # character before i store the answer and put it in later when i need it?

But actually when i check the file before it gets sent over sometimes it has two hash symbols like ##FFFFFF I cant help this but maybe it will help the solution.

any ideas?

other code ive tried with it is

<?php
$colorpreference = $HTTP_GET_VARS["userinfo"];
$ip = getenv ('REMOTE_ADDR');
$file = fopen('colorpreference .txt', 'a');
fwrite($file, $colorpreference . "\n\n");
header ("Location: /index.php"); //<-------After it redirects to another page.
?>

<?php
$f = fopen(“colorpreference .txt”, “a”);
fwrite($f, “IP: {$_SERVER[‘REMOTE_ADDR’]} Ref: {$_SERVER
[‘HTTP_REFERER’]} Data: {$HTTP_GET_VARS[‘userinfo’]}\n”);
fclose($f);
?>

But yet again the same problem it doesnt store anything after the #

Sane May 7th, 2006 3:51 PM

I believe that's because you don't want to pass that kind of data through the URL. The URL GET variables generally don't like certain things, like "#" or "&" or "?" as it will mistake them for certain things (# is an id referencer, & means next attribute, ? triggers the GET statements). Your PHP script stops reading the variable "userinformation" once it hits the '#', because it interprets that as an ID identifier.

Your two options, are...

1) Instead of passing a pound sign through, pass the ascii equivelent: '%23' just as '%20' represents as whitespace character, '%23' is the pound sign. PHP will automatically convert it back to a pound sign.

2) Use POST data. Whatever form you're using to access the PHP script, turing the "action" from "GET" to "POST". Then in your PHP script, use the variable $_POST['userinformation'] rather than $_GET['userinformation'].

Make any sense?

DaWei May 7th, 2006 4:58 PM

You didn't discover the problem, Columbus did. See the PHP manual, urlencode. Sane's suggestion of POST data is good, too.

dunowhodoyou May 8th, 2006 4:39 AM

Thanks sane that brilliant

I can't change they way the data is sent so cant use urlencode but the post one seems like it should work ... again im not the best at php so ill read up about $_post and figure out how to put it in the code ...

is there anyway to use a getnext so it stores the first half and then comes to the pound and then use another get to retrieve the info after the pound?

thanks again

dunowhodoyou May 8th, 2006 5:10 AM

Just had a look into post and im passing over there preferences of my site in a cookie, thats the main thing in my project, so i cant actually use a post because im using

test2.php?userinformationc='%2Bdocument.cookie)

so i cant put a post in the url ... im starting to think this was a bad project haha even though im 99% complete im starting to think the other 1% is impossible

DaWei May 8th, 2006 5:37 AM

Using urlencode merely encodes troublesome characters. You use urldecode on the other end. You are coding the data in a different way, so that's technically 'changing' it, but you aren't changing the content, just the form. What do you think encryption/decryption does? I don't know whether it's you or your client/teacher/boss, but someone is being silly.

dunowhodoyou May 8th, 2006 6:47 AM

the whole nature of the project is to get the preferences sent over in the cookie , and it works perfect apart from the # symbol so i could change the way the colour is stored but id love to find a way around it

test2.php?userinformationc='%2Bdocument.cookie)

Is the ONLY way i can send the information i cant really change anything there so its in the retrieve part that its having the problem ... the full url with the request is in the url bar and the #FFFFF is showing and some more info after it but just when i try to store it into a text file it stops at the # .... is there anyway i can tell it ... retrieve the first part , then the second part is seperated by the # symbol and retrieve the second part?

And like i said im terrible at php so if anyones being silly its me haha

Arevos May 8th, 2006 7:32 AM

How is the data being passed into your program? If you have control over both ends, surely it's just a case of urlencoding the URL variables, and then urldecoding them at the other end.

dunowhodoyou May 8th, 2006 10:06 AM

The whole nature of the project was to extract it from there cookie, and if worst case sinario happens then yea i can urlencode it but im just trying to see if theres a way around with without changing the webpage side of things

DaWei May 8th, 2006 10:29 AM

The point is that the "webpage side" should already be using urldecode. One never knows what a user will type in. Urldecode won't kill things if there's no urlencoding there. If you have chosen to put troublesome characters in the url, and choose to be recalcitrant about handling it with the appropriate and already-provided tools, well, there ya go.


All times are GMT -5. The time now is 9:57 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC