I used to be able to post variables to pure PHP pages, but it seems to have been broken by a server setting change with our web host, because this used to work just fine. Basically, I've set up a form to retrieve and allow the user to download a file from a secure location outside of the public folder on the server (only to authorized users). It works like this:
request_file.php
<?php
session_start();
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Request File</title>
</head>
<body>
<form name="request_file" method="post" action="retrieve_file.php" enctype="multipart/form-data">
<input type="text" name="filename" />
<input type="submit" value="Submit" />
</form>
</body>
</html>
retrieve_file.php
<?php
session_start();
header("Vary: User-Agent");
header("Pragma: public");
header("Content-type: text/plain");
header("Content-Disposition: attachment; filename=".$_POST['filename']);
readfile( "../../".$_SESSION['userid']."/".$_POST['filename'] );
?>
However, if I replace the retrieve_file.php code with simply this:
<?php
echo "<pre>";
var_dump( $_POST );
echo "</pre>";
?>
It will print out an empty array, every time. However, if I put HTML headers on retrieve_file.php, it works just fine. However, for exporting a file, this won't really work since the headers have to be sent out AFTER the page gets its post variables.
Any ideas how to work around this? Please don't berate me for pulling files this way, it actually works well for our very specific application.
Thanks!
Additional Information
PHP Version: 5.4
You need to access with the $_FILES variable, like this:
$_FILES["file"]["filename"]
Check more at http://www.w3schools.com/php/php_file_upload.asp
Related
Background: I am building an Ajax page which will be called by a user's browser to retrieve data already loaded in $_SESSION. I want to test that the Ajax page works properly so I am trying to do a print_r($_SESSION) on it. (Note: the print_r() works fine on the main site.)
Problem: I cannot get my PHP to produce valid HTML. If I do:
<?php
header("Content-Type: text/html; charset=utf-8");
echo '<?xml version="1.0" encoding="utf-8" ?>';
if (!session_id()) {
session_start();
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" /> <!-- See http://www.w3.org/International/O-charset.en.php-->
<title>Test for Ajax</title>
<link rel="stylesheet" media="screen" type="text/css" title="StyleSheetProjet" href="StyleSheetProjet.css" />
</head>
<body>
<pre>
Content of $_SESSION:
<?php print_r($_SESSION) ?>
</pre>
</body>
</html>
In which case the page shows:
Content of $_SESSION:
Array
(
followed by a bunch of __PHP_Incomplete_Class Object (because the objects in session have not been declared I assume).
However if I start with:
header("Content-Type: text/html; charset=utf-8");
echo '<?xml version="1.0" encoding="utf-8" ?>';
//Obects in session:
require_once("Class_User.php");
require_once ('Class_Message.php');
if (!session_id()) {
session_start();
}
//(Rest is same as above)
Then the browser renders nothing at all, not even Content of $_SESSION:. In fact, the html in the web console is simply <html><head></head><body></body></html> with nothing in between...
What am I doing wrong?
Edit 1: to replace text/xml in the header() with text/html.
Edit 2: I replaced the two require_once with:
if (file_exists('Class_User.php')) {
echo "Class_User exists";
} else {
echo "Class_User doesn't exist";
}
if (file_exists('Class_Message.php')) {
echo "Class_Message exists";
} else {
echo "Class_Message doesn't exist";
}
The page does return Class_User existsClass_Message exists so it clearly can find the files.
Suspected Reason
require_once("Class_User.php"); and require_once("Class_Message.php"); are looking for the related files, cannot find them, so stop executing the rest of the code. Check if the path to Class_User.php and Class_Message.php are correct.
Actual Reason (we found working together w/ the OP after debugging)
One of the class files was extending another class and since that file was not included in the project the execution was being blocked. The OP solved the issue by calling another require() for this third class.
I am currently trying to implement a PHP page that will generate a code when a user clicks a button. The code will be generated on the fly, and then the PHP page will trigger a download file that will prompt the user to save the text file on their machine.
After reading numerous posts, I am using this bit of code which is short, concise and does exactly what I need:
{
$result = "Your code is: " . $_POST['req_code'];
header('Content-disposition: attachment; filename=code.lic');
header('Content-type: text/plain');
echo $result . "\n";
}
However, the downloaded file does not only contain the code, it also contains all the HTML from the current page. Below is just a fraction of what is included in the file:
<!DOCTYPE html>
<html lang="en-CA">
<head>
<meta charset="UTF-8" />
<title>My Account | ...
I have tried using the "exit();" function to terminate the stream but it still outputs all the HTML.
Does anyone know what I'm doing wrong here? The file should only contain the contents of $result.
Thank you in advance.
Although this is an old thread, there are search results that still direct to here. So to answer the original question (sort of) by giving a simple stand-alone example that works, here goes:
Create two files, 'index.php' and 'redirect.php'
index.php:
<?php
$head = <<<EOHEAD
<head>
<title>Download document test</title>
<meta http-equiv="Content-Type" content="txt/html; charset=utf-8" />
</head>
EOHEAD;
$body = <<<EOBODY
<body>
<form action="redirect.php" method="post" id="mainform">
<input type="submit" name="download" value="Download"/>
<input type="submit" name="cancel" value="Cancel"/>
</form>
</body>
EOBODY;
echo $head.$body;
?>
redirect.php:
<?php
if (isset($_POST['download'])) {
header('Content-disposition: attachment; filename=test.txt');
header('Content-type: text/plain');
echo "Hello World\n";
}
else {
header("location:index.php");
}
?>
You need to either a) Make a new page to download the file (without any HTML) or b) Remove all HTML from the current page. Also, you are not supposed to send any content before calling header():
Remember that header() must be called before any actual output is
sent, either by normal HTML tags, blank lines in a file, or from PHP
Reference: http://php.net/manual/en/function.header.php
You do not have to delete all HTML code from download site, but there cannot be ANY HTML code before function, that downloads your files. Put your code ad the top of the page and it should be fine
I am just calling a simple PHP script through a HTML form.
An error is thrown everytime : "The character encoding of the HTML document was not declared. The document will render with garbled text in some browser configurations if the document contains characters from outside the US-ASCII range. The character encoding of the page must to be declared in the document or in the transfer protocol"
I have defined the encoding both in PHP as well as HTML as UTF-8 (please refer to code below). I am unable to solve this problem despite searching all over the web.
HTML code :
<head>
<meta http-equiv="Content-type" content="text/html; charset=UTF-8">
<meta content="UTF-8" http-equiv="encoding"/>
<title>Test</title>
</head>
<body>
<div align="center">
<form action= "google1.php" method="get" accept-charset="UTF-8" >
Enter Your Name:
<INPUT TYPE = "text" NAME = "student"> <BR>
<!--input name="q" type="text"-->
<br/ >
<input name="btnG" type="submit" value ="test">
</form>
</div>
</body>
PHP Code
header("Content-type: text/html; charset=UTF-8");
print "<pre>";
print_r($_GET);
print "</pre>";
The result after submitting the button (along with the error) is :
"; print_r($_GET); print ""; ?>
I am using XAMPP. I tried to edit .htaccess (added : AddType 'text/html; charset=UTF-8' html) as suggested in some of the solutions over internet but that also did not help.
I found a site where there is a simple form which calls a PHP script again. http://www.tjhsst.edu/~dhyatt/superap/form1.html . When I try to submit value in the form, I get the same error.
So I thought, this could be a browser problem and I changed the default encoding of my browser to UTF-8. But this also did not help.
I am a novice in web programming and trying to learn. Appreciate if any one can help.
Thanks,
Ashutosh
It looks like you have some issue with opening/closing tags.
Ensure that you have php code wrapped with <?php and ?> in your process1.php3 (Some details: http://php.net/manual/en/language.basic-syntax.phpmode.php)
Like here:
<?php
header("Content-type: text/html; charset=UTF-8");
print "<pre>";
print_r($_GET);
print "</pre>";
?>
UPD:
After a long session of question/answer finally appeared that OP were opening file with a form using file:// protocol. Like file:///C:/xampp/htdocs/example/form.html and form were submitted to file:///C:/xampp/htdocs/example/google1.php?... As apache works with HTTP protocol only, PHP were not executed actually.
Your Code:
<meta http-equiv="Content-type" content="text/html; charset=UTF-8">
Correct code:
<meta http-equiv="Content-type" content="text/html;charset=UTF-8" />
You have forget to put slash at the end of meta-tag. You have not closed the meta tag.
Though, its not very crucial, as you have tried everything. Try this one too. It might work for you.
I'm using php to build a small form that requires passing 2 variables for processing.
I got the example off w3schools and although the info gets passed in the URL, the php form doesn't process it in any way (let alone extract it).
I'm just wondering if there might be anything wrong with my WAMP server.
<html>
<body>
welcome
Welcome <?php echo $_GET["fname"]; ?>.<br />
You are <?php echo $_GET["age"]; ?> years old!
</body>
</html>
HTML form:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http:// www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>
<body>
<form action="welcome.php" method="get">
Name: <input type="text" name="fname" />
Age: <input type="text" name="age" />
<input type="submit" />
</form>
</body>
</html>
Indeed as already noted by various posts, this code should work.
In my experience the php tags are output directly if they're not parsed.
Do you see anything on the page at all? You could also check the sourcecode of the page and check if you can spot anything wrong there.
Also when copying from examples on the internet, sometimes you get weird characters that interrupt the parser. Usually this results in an error (which is not the case here), there's no harm in checking though.
Try outputing something simple and see if that works:
<?php echo "Hello World"; ?>
Can't think of anything else at the moment...
This should work fine code-wise. Did you save the php file in the same directory as 'welcome.php'?
Your code seems correct. Does PHP work? Does
echo phpinfo();
work? If yes, what does
var_dump($_SERVER);
give you? Do the GET parameters appear there?
Just add <?php echo phpinfo(); ?> after that run your page for example page http://localhost/yourpage.php again, now just see whether error_reporting = E_ALL and display_errors = 1 is set or not if not then you have to configure your php.ini,
Check URL if you can view values in url and still can't able to fetch it then try var_dump($_REQUEST); and my suggestion is to try to submit form in same page because when i was fresher i write both codes in same page but action page is different so i was confused why its not working
This script should work as far as I can see. What is your WAMP-setup?
Try to upload the script on a free server that uses php and run it there.
I have a test.php page
in this page , there is a url . i need to execute that url (here database updation is doing).This is my code
<?php
$username ='testUsername';
if($_GET['age']!=''){
header('location:www.test.com/update.php?age='.$_GET['age'].'&username='.$username); //need to updatethe age of this username
$show ='hello';
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>
<body>
<?php echo $show ?>
</body>
</html>
i know this code is not going to work properly.How can i write in a good way.I donot want to redirect the page.I just need to execute that link
Just call file_get_contents on the url in question instead of setting the location header to that value.
Since the update.php file is hosted on an external website, the only thing you can do is get the contents of the output of the file. (Use file_get_contents to get that output.) That is, it will call the file (with your parameters) and you can fetch the HTML result of it—nothing more. It would be a major security problem if server files could be executed on external websites.
If you need to include the code in update.php without redirecting, you can do so with the include or require functions.
require() is identical to include()
except upon failure it will ... halt
the script whereas include() only
emits a warning (E_WARNING) which
allows the script to continue.
Use the IMG tag. <img widht=0 height=0 src="<?='location:www.test.com/update.php?age='.$_GET['age'].'&username='.$username ?>" />