Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old May 7th, 2006, 4:39 PM   #1
dunowhodoyou
Newbie
 
Join Date: May 2006
Posts: 5
Rep Power: 0 dunowhodoyou is on a distinguished road
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 #
dunowhodoyou is offline   Reply With Quote
Old May 7th, 2006, 4:51 PM   #2
Sane
Programming Guru
 
Sane's Avatar
 
Join Date: Apr 2005
Location: Waterloo, Ontario
Posts: 2,013
Rep Power: 6 Sane will become famous soon enough
Send a message via MSN to Sane
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?
Sane is offline   Reply With Quote
Old May 7th, 2006, 5:58 PM   #3
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
You didn't discover the problem, Columbus did. See the PHP manual, urlencode. Sane's suggestion of POST data is good, too.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old May 8th, 2006, 5:39 AM   #4
dunowhodoyou
Newbie
 
Join Date: May 2006
Posts: 5
Rep Power: 0 dunowhodoyou is on a distinguished road
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 is offline   Reply With Quote
Old May 8th, 2006, 6:10 AM   #5
dunowhodoyou
Newbie
 
Join Date: May 2006
Posts: 5
Rep Power: 0 dunowhodoyou is on a distinguished road
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
dunowhodoyou is offline   Reply With Quote
Old May 8th, 2006, 6:37 AM   #6
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
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.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old May 8th, 2006, 7:47 AM   #7
dunowhodoyou
Newbie
 
Join Date: May 2006
Posts: 5
Rep Power: 0 dunowhodoyou is on a distinguished road
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
dunowhodoyou is offline   Reply With Quote
Old May 8th, 2006, 8:32 AM   #8
Arevos
Programming Guru
 
Arevos's Avatar
 
Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 5 Arevos is on a distinguished road
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.
Arevos is offline   Reply With Quote
Old May 8th, 2006, 11:06 AM   #9
dunowhodoyou
Newbie
 
Join Date: May 2006
Posts: 5
Rep Power: 0 dunowhodoyou is on a distinguished road
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
dunowhodoyou is offline   Reply With Quote
Old May 8th, 2006, 11:29 AM   #10
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
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.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 6:39 AM.

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