- Home /
Help with WWWForm, PHP and permissions...?
Greetings all,
I have searched online and found some info on using PHP/MySQL with Unity (like the Server Side High Scores), and several Google searches don't seem to get me closer to what I'm trying to do. Maybe I'm not quite hitting the right search terms, so I thought I'd ask here. Though I'm new to PHP, I think I have a good grasp on how the PHP scripts work on the server side.
Ultimately, I'm trying to test a proof of concept to incorporate into a project. The goal is to click a button in a web player and have it spawn a new window. The button should send two strings containing a name (e.g. John Doe) and an ID code (e.g. 12345) via a WWWForm using the $_POST command in a PHP script.
The PHP script will take the name and ID code and generate a .xfdf (XML Form Data File) via another script (createXFDF PHP script by Justin Koivisto found here if you are really interested) which combines the XFDF data with an Adobe PDF form that has 3 fields: name, ID code, and date. The date is pulled from the local system and inserted into the form while the other two should be passed as POST data. The final result is a printable certificate of completion.
I am able to spawn a new browswer tab using Application.OpenURL. I attempt to pass the name and ID code from my simple Unity project as WWWForm fields:
void Start ()
{
WWWForm form = new WWWForm();
form.AddField("userName", userName, System.Text.Encoding.UTF8);
form.AddField("userID", userID);
www = new WWW(url, form);
}
When I click the button in the Unity GUI, I spawn the new browser tab and start a Coroutine that waits for the WWW request:
<?php
session_start();
$name = $_POST['userName']; //"John Doe";
echo $name;
$userID = 12345;
echo $userID;
$_SESSION['name'] = $name;
$_SESSION['userID'] = $userID;
?>
The code above shows 12345 and the GetPDF button in a the newly created browser tab(replicated below since I don't have a URL to host images handy):
12345
[Get PDF]
NOTE: The $name passed in the $_POST doesn't appear to be coming in.
When I look in the Unity Editor Log, I did find this:
WWW Ok!: <!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
Authorization Required
This server could not verify that you are authorized to access the document requested. Either you supplied the wrong credentials (e.g., bad password), or your browser doesn't understand how to supply the credentials required.
I'm using Unity 3.4.2f3 and The Uniform Server 7.1.15-Orion (to run PHP 5.3.8 and Apache 2.2.21 as a local testing server) running on a Windows 7 Professional 64-bit system.
I have also tried my scripts on a Windows Server 2003 system running IIS 6.0 and PHP 5.3.8 since I originally had issues with permissions but I'm seeing similar results.
I thought it is a permissions issue, but I am an administrator on my system and those PHP scripts are all running on the server side. Also, just for grins, I tried passing the string "John%20Doe" as part of the URL and changed the POST to a GET. This actually echoes the string correctly and therefore correctly completes the XFDF file and resulting certificate.
So, after all that, if you are still reading, I guess the real questions are, can anyone tell me what format the AddField strings are being sent to the PHP script? Or, is the userName field always coming up blank due to the HTTP 401 error? And, why would the GET work but the POST not? Are they not subject to same/similar security constraints?
Sorry for the long post, I wanted to give sufficient background and I hope this is enough to get someone to point me in the best direction. Thanks in advance to any kind soul willing to help me.
Looks like I'm 0 for 2 on UnityAnswers... (sigh) Anybody? Anybody? Bueller...
Well, in the case that my question and follow up may help anyone...
After some more searching, I found that I had uncommented a few lines from the .htaccess file to tighten up security. This seems to be the cause of the 401 Authorization Required.
Since commenting those 4 lines out again, I stopped getting the 401 error and the server returns the HT$$anonymous$$L, but the $_POST command still doesn't seem to accept the data and ultimately reach the certificate.
For the moment, I'm using the GET command, but that's not the solution I want.
Can anyone with experience connecting Unity with PHP point me in the right direction?
$$anonymous$$y next challenge is getting a simple login using Unity->PHP->$$anonymous$$ySQL.
Check out the PHP example on the wiki. It was designed to be a complement to the docs for WWWForm.
Answer by rutter · Apr 14, 2012 at 03:23 AM
The error probably means exactly what it says: your webserver is blocking the connection because of some permissions issue. If this is not the intended result, you should double check your webserver's configuration.
You mention you're using Apache. You may want to check httpd.conf
or htaccess
files that block access to POST requests. You can see some examples referencing user auth settings here, or search around the net a bit. In particular, you'll want to check for directives like LIMIT
and require valid-user
, and consider making some changes to the requirements and/or limited features. Turning off or tweaking these security settings is not necessarily a good idea unless you're very sure of what you're doing, so be careful.
If that doesn't turn up any results, you should check your server's connection and error logs for any clues.
I haven't logged in here for a while, so I just saw this. The project requirements changed and the need for a login and password was dropped. Thanks for the suggestions.