is it normal to send special characters encoded in post - php

I had a discussion with someone and could not come to a proper solution so I wanted to know how you guys think about this:
I have a html form and the other guy call him 'Aron' has got a .net system.
My html form has a input text field called description.
Aron his .net system catches my description Post and then changes this data in to XML.
BUT if a special character like & is posted, then he will get a parse error.
Now Aron is telling me that i need to post the & data as & and not as raw &.
What do you guys think about this?

For me it sounds more like a server-side problem. If I were you, I would create an object, serialize it to JSON, send it to this .NET application and let .NET developer do whatever he needs with it. I have sent proper data in accordance with my arhitecture and language.
It would be more proper and reasonable. Imagine the case - you don't work with Aron anymore, you work with Mark who takes your POST data and saves as plain text. He will ask "why are you sending HTML-encoded data to me? I do not need this". You definitely won't tell him "just decode it back" and you definitely don't want to change your code every time you change a partner. What if you work with both of them at the same time, or with 10 services at the same time?
As a consumer of a service, you should not bother about how this service is implemented. Moreover, you may now know how it is implemented, and it shouldn't affect your code.

I think the honourable gentleman Aron should use an appropriate library that deals with issues such as this and more, used correctly it should be almost automatic.
Failing this, he can use System.Web.HttpUtility.HtmlEncode() before converting to XML.
It can be done client side, using javascript to fiddle with the data before it gets sent... but this in my opinion is bad practice and should be handled by the server.
An alternative is encoding the html entities using htmlspecialchars() in PHP yourself, then using CURL to post the data to his ".net system".

Related

Simple PHP web crawler to submit form and store the returned results

For a system I am developing I need to programmatically go to a specific page. Fill out one field in the form (I know the id and name of the input element), submit it and store the results.
I have seen a few different Perl, python and java classes that do this. However I would like to do this using PHP and havent found anything as of yet.
I do have the permission to do this from the site i am getting the information from as well.
Any help is appreciated
Take a look at David Walsh's simple explanation.
http://davidwalsh.name/curl-post
You can easily store the response (in this example, $result) in your database or logfile.
Usually PHP crawlers/scrapers use CURL - http://php.net/manual/en/book.curl.php.
It allows you to make a query from the server where PHP runs and get response from the website that you need to crawl. It returns response data in plain format and parsing it is up to you. You can manually check what does the form submit when you do it manually, and do the same thing via curl.
You also may try phpcrawl (http://phpcrawl.cuab.de), seems to fit all your needs.
(See "addPostData()"-method)

base 64 encode a form file

I'm writing a very simple file sharing site in JS and PHP. I've got a drag/drop working, so the browser gets a file object upon drop, and from there I tried to send in a xhr request to an upload page. However, I can't seem to just drop a binary file object in a request header, and so was wondering how I'd go about base64 encoding it.
In PHP I'd use base64_encode, but I'm not even at the PHP page yet. Maybe you could suggest an alternative method to my current one?
Oh, and in the PHP that receives it, it writes to a semi-random file after base64_decodeing the file.
EDIT: I worked around it, but there isn't really a good answer. Thanks for helping!
Here's my demo: http://bernsteinbear.com/up
There is a function in the works that is currently only supported in Firefox, xhr.sendAsBinary, but for now you can do the Base64 encoding in Javascript with this custom function:
http://www.webtoolkit.info/javascript-base64.html
Alternatively, you can implement sendAsBinary yourself, as seen here:
http://hublog.hubmed.org/archives/001941.html
Just be aware that the Base64 method is currently the most compatible method.
Is there any reason you aren't using something like the Valumns File Uploder? I don't know why you're wanting to add a binary file "as a request header" (that makes little sense to me), but having to base64 encode it before sending seems a bit silly when HTTP can handle sending binary data in both directions quite easily (example with forms). Then again, I'm unfamiliar with the File API, so I'm not sure what special things you might be doing with it (are you transforming the file at all before sending?). Maybe I'm missing the point of this exercise.

HTML Entities Converting Very Strangely

I am working on a project that involves modifying some existing code and there is a behavior going on that makes absolutely no sense to me. I am hoping somebody has seen something similar and thus can provide some insight as to where the problem is originating from.
The best short example I can give is the following:
A user enters "This & that" into a textarea on an input form and when saved
once it becomes: "This & that", when it is saved again it becomes:
"This & that", save it again and you get:
"This & that".
Obviously the problem continues to get worse with each save. The data actually stored in the DB (MySQL) is the text displayed above, there are no filters on the front-end to convert characters/entities. Obviously if they were being stored properly it would be very easy to slap a call to htmlspecialchars_decode() but that isn't an option yet...
Are there some front-end checks I can be doing to see where the symbols are being mangled? I am looking at the controller that processes the data and it's using a rest event to do so but no where do I see anything that would even try to convert the HTML entities, let alone something that would incorrectly convert them.
As I said in the intro, I hope somebody may have seen this before and can help pinpoint where it might be happening. This is built using PHP (Protean, MVC framework), Propel, patforms/smarty are in play, MySQL (via PDO) on the backend, jQuery for most JS-related stuff.
Your data is being htmlentities() too many times. This is a common, noobish mistake that usually involves urlencoding your data before sending to the database, and encoding it again upon retrieval. Once (on output) is enough. You should never encode it going in.
I hate to answer my own question here but it was in fact a bi-product of a set method buried in the framework that was causing the double encoding. I changed the data flow a bit and now everything is being stored properly and I can now just throw a htmlspecialchars_decode() around the output and life is good.
Thanks for the suggestions everyone!
-- N

Rendering a page in PHP: How?

This may be a inappropriate question for SO, but I thought lets see :)
I'm writing a website in php. Every pageload may have 10-20 DB requests.
Using the result of the DB queries I need to generate a page.
The page would contain a topic (should be image or text) followed by comments. There could be mutiple topics like this.
Currently, I'm creating a string using the DB result and sending it to the browser.
When browser receives the string (as an ajax response), it parses using split functions and creates the HTML dynamically.
I'm basically a C++ programmer; relatively new to web development. So, I do not have fair understanding of the JS objects. How long of a string can JS variable hold? Is it ok to use split and generate HTML at the client.
I'm not generating the complete HTML at the server side to avoid any overhead because of string concatenation. I believe sending less no. of characters to the client (like I'm doing) is better as compared to sending complete HTML code.
Is something (or everything) wrong in my understanding :)
Any help is appreciated.
EDIT:
Well, I'll be highly grateful if I could get opinions in yes/no. What would you recommend. Sending HTML to the client or a string that will be used at the client to generate HTML?
Unless you have a specific reason for doing so, I think you should look into generating the HTML with PHP and sending it directly to the browser. PHP was built specifically for this purpose.
I think you be best off to look at jQuery and more specific to the AJAX method of that library. Also, take a look at JSON and you should be all good to go.
Have you considered using a templating engine like Smarty?
It's pretty simple to use, take a look at the crash course, you might like it! http://www.smarty.net/crash_course

Real time RSS display on web page (best practices and source codes)

i have a php script who parser a rss and give me the data in a know pattern. Im very new with ASP, JavaScript and Jquery so i dont have any idea of how to autoupdate the script and display the new data with a smooth animation (see this example, that exactly what i want). Thanks for the support and if you know a good script to made this i will appreciate it.
Seems like you're looking for this:
http://leftlogic.com/lounge/articles/jquery_spy2/
It's PHP (not ASP), so that might be an issue, though the code is SUPER easy to implement (I've written by own implementation on three separate occasions).
The site itself has some decent documentation on getting things up and running, but if you need some extra help, comment and I'll point you in the right direction :)
Good luck!
The resources people have linked here are helpful and merely mentioning jQuery means you're probably headed in the right direction. But if you're new to this it might still be worth mentioning some of the concepts you'll be looking to play with here.
First of all, you'll probably want to stick with one language on the client side and one on the server side. This means choosing either PHP or ASP -- this isn't clear from your question but I'll assume you're dealing with PHP since that's the language I use for this kind of thing. JavaScript + jQuery is the right choice for the browser (client) side of things.
Like Luca points out, you'll have to set up some JavaScript code that goes live on page load and "polls" the server at a set interval. In JavaScript you do this using something called XMLHttpRequest (or "XHR") and it's pretty complicated. You could use combination of jQuery and a library like the one Matt points to in his answer, or just jQuery -- sample code abounds but it's basically a loop with a function call and sleep timer.
That function call is going to be one of the more difficult parts if you're trying to emulate the Twitter World Cup site. But here's the basic idea: You need to populate a list using jQuery and a data standard like JSON. Since the RSS feed you'll be parsing is written in XML, you'll have to write a server side (PHP/ASP) script that fetches, parses and converts the feed to JSON. In PHP, this is best done through cURL (file_get_contents() if you're lazy), SimpleXML and json_encode(), respectively.
Your JavaScript should load the list based on JSON. To do this, and display any new items, what you'll do is load the JSON from the client (browser) side using a jQuery method like getJSON(). Then you spin through the array object and add any new items to the list by adding new <li> elements to the "DOM." The same jQuery code that does this can easily also do the cross dissolve with something like fadeIn().
It looks like the script on that example page has an Ajax request running every TOT seconds.
You could simply have your PHP script return the RSS data (in JSON format say) and let JavaScript parse it and generate some HTML with it.
If all of this doesn't make sense to you I advice reading a little about JavaScript and PHP... there's plenty of good books.

Categories