November 2, 200917 yr Hi all Can anyone shed some light on this? I have a very simple PHP script to pipe form information to another page. The problem is, it only works on linux hosting, but the site I need it for is on Windows. Both hosts are running php5. My host is being totally ignorant and saying they don't provide scripting support. But as far as I can see, my script is fine. Anyone spot the cock-up? <!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>Search</title> <?php if ($_POST) { $searchterm=$_POST['searchtext']; $admin=$_POST['admin']; if ($admin) { header("location:business.all.asp?search=$searchterm"); } else { header("location:business.asp?search=$searchterm"); } } ?> </head> <body> <p>If automatic redirection fails, <a href="business.asp?search=<?php echo $searchterm; ?>">click here to see your search results</a>.</p> </body> </html> Thanks
November 2, 200917 yr I'm not good enough with PHP to spot anything but obvious errors. But could it be, as it's in an HTML document, that you need to add an addhandler into your htaccess so your server parses HTML as PHP? I had to do that. I try to help I really do!
November 2, 200917 yr I'm not sure if this will fix it but try putting your PhP before your doctype. It shouldn't make a difference but some times it does. I usually try echoing variables to see if they are coming across from the form.
November 2, 200917 yr Try this and put this code block top of the page, above doctype declaration. <?php if (isset($_POST['searchtext'])) { $searchterm=$_POST['searchtext']; $admin=$_POST['admin']; if ($admin) { header("location:business.all.asp?search=$searchterm"); } else { header("location:business.asp?search=$searchterm"); } } ?>
November 2, 200917 yr As has already been mentioned, make sure you use header() before any other output. Linky
November 2, 200917 yr Author Thanks for your replies so far all. I have moved the block of code to the top of the page before any html as advised, but still no joy... The error that IIS gives me is: HTTP/1.1 500 Internal Server Error Server: Microsoft-IIS/7.0 X-Powered-By: ASP.NET Date: Fri, 30 Oct 2009 16:52:09 GMT Connection: close Content-Length: 382 PHP Notice: Undefined variable: admin in \\xx.xx.xx.xx\home\domain.com\public_html\business\business_search.php on line 12 PHP Notice: Undefined variable: searchterm in \\xx.xx.xx.xx\home\domain.com\public_html\business\business_search.php on line 16 PHP Notice: Undefined variable: searchterm in \\xx.xx.xx.xx\home\domain.com\public_html\business\business_search.php on line 21 Thanks for any more suggestions.
November 2, 200917 yr Can you post the form just in case there is any mistakes in there. Although if it is working in linux there probably isn't any.
November 2, 200917 yr Author I don't think there's anything wrong with the form, but here it is... <form name="search" method="POST" action="path_to_script.php" enctype="multipart/form-data"> <label for="search">Local Business Search</label> <input id="searchtext" type="text" name="searchtext"> <input id="submit" type="submit" name="Submit" value="Search"> </form>
November 2, 200917 yr Does your windows host have fastcgi enabled for iis, its worth a try, ive read that if this is not enabled iis and php dont get along to well
November 2, 200917 yr Author I might be missing something but where is $_POST['admin'] coming from? $_POST['admin'] comes from another form via a logged in page. I haven't even got around to testing that yet, but as it's not a public page, I'm not so bothered. My host seems to think there's nothing wrong with their server config, and keep pointing me to the server logs... grrr
November 2, 200917 yr would that not need to be set as a session variable rather than post or do both forms go to this script. have you tried sending the form to just echo the variable. something like. <?php echo $_POST['searchtext']; ?> if that doesn't work then there must be something wrong with the host.
November 2, 200917 yr Author The other form points to the same script, yes. I can't even get IIS to do anything with this script. It won't echo the $_POST data, it doesn't even seem to recognise the form data. Why else would it fail like this? The error undefined index appears to be related to missing form data in some instances, but my form passes data fine on linux. So frustrating as the whole reason for this script is that the .asp script which does the same thing will not work with legacy (Panther and earlier) macs on Safari. Some weird conflict there too... @pat I don't know it fastcgi is installed, I'm off to have a look now.
November 2, 200917 yr There is a cure but you would have to get your host to alter the php.ini file which is unlikely to error_reporting = E_COMPILE_ERROR|E_RECOVERABLE_ERROR|E_ERROR|E_CORE_ERROR
November 2, 200917 yr Hi I took a look at your code and I'll give my thoughts: Firstly, I would remove all HTML content from the page, so just have a PHP page with the following text in it: <?php if ($_POST['submit') { $searchterm=$_POST['searchtext']; $admin=$_POST['admin']; if ($admin) { header("location:business.all.asp?search=$searchterm"); } else { header("location:business.asp?search=$searchterm"); } } ?> secondly, I have added 'submit' to the top of the IF statement so it now reads: if ($_POST['submit') This is so it know when to run the script (when the submit button is pressed) lastly, in the first part of the IF statement the redirection goes to 'business.all.asp', surely having the .all.asp would confuse the server into what the filetype is? maybe change the file to business-all.asp? Just my thoughts.. Chris
November 2, 200917 yr Author Thanks for the suggestions - adding ['submit'] to if ($_POST) has not made any difference, and I know that business.all.asp is not a problem as this page renders properly once the data is sent to it.
November 2, 200917 yr if ($admin) { header("location:business.all.asp?search=$searchterm"); } else { header("location:business.asp?search=$searchterm"); } Probably irrelevant, but this else will never be reached? Your saying if $admin, which will always return true because you are setting it right above it everytime. Even if $admin is empty, it will still return true.
November 2, 200917 yr Put this to first line in PHP block and at least get rid from notifications. There are some undefined variables like in system response. ini_set('display_errors', 0);
November 2, 200917 yr Author Thanks for your replies. The else statement is reached on linux hosting; the script works very well. As for adding error reporting to the script, there's no point as the server is configured to show a 500 error page instead of any output from php. I'm beginning to feel a naming and shaming exercise coming on....
November 3, 200917 yr I've had issues with posting in PHP on IIS (version 6 I believe). I couldn't post to dynamic URLs or non PHP files, it had to be the absolute PHP file (i.e. thefile.php and not thefile.php?something=1). If possible try and get Friendly HTTP Errors enabled and see if you can get a more specific reason for the error, althought I doubt it will help. You could also see if you can log error messages to the filesystem: ini_set("error_log", "error.log"); If all else fails (and it's an option) use Apache as opposed to IIS. I've had a lot of problems with IIS/Windows and Apache/Windows and find Apache/Linux to be more flexible and hassle free, especially for PHP. Edit: Try phpinfo and check the POST settings. Could be something as simple as that (if you haven't already had a look).
November 4, 200916 yr Hey !b, Are you able to get GET data through? I'm not sure exactly what this would mean, but it might help narrow down the problem. What happens if you access the script by going to "path-to-script.php?test=test" and then put a line somewhere at the top of the script saying: if ($_GET['test']){ echo 'GET variables are working';} But apart from that I'm not sure. Tis a tricky one.
November 4, 200916 yr Here's something to make you feel better: http://www.fullduplex.org/humor/2006/10/how-to-shoot-yourself-in-the-foot-in-any-programming-language/
November 5, 200916 yr Author Hey Gomez - thanks for your suggestion. I'll try that next week when I get back to work, and post back.
November 9, 200916 yr Author Hmmm, GET variables do seem to be working, and could provide a nice solution to my problem but for 2 points: 1 - There is no reason for POST variables to not work 2 - the form appears on every page of the site, so it's impractical to have the script duplicated that many times... Anyone else got any other suggestions? - ones that don't involve moving to Linux hosting which I will do when I rebuild the site, but for now I am stuck with windoze as it's an asp site with a fricking access database ffs...
November 11, 200916 yr What you're probabaly experiencing is a simple "undefined variable" error and maybe IIS is configured to display an http error when it encounters one. Try this. And ALWAYS remeber to use exit() to terminate the further execution of the script otherwise code afterwards will still be ran on the server still. <?php // Show all errors and work in development like this.. always. error_reporting(E_ALL); $submit = (isset($_POST['submit'])) ? $_POST['submit'] : FALSE; $searchtext = (isset($_POST['searchtext'])) ? $_POST['searchtext'] : FALSE; // Full url $uri = rtrim(dirname($_SERVER['PHP_SELF']), '/\\'); $host = $_SERVER['HTTP_HOST']; if($submit) { if ($admin) { header('Location: http://'.$host . $uri.'/business.all.asp?search='.htmlspecialchars($searchtext)); } else { header('Location: http://'.$host . $uri.'/business.asp?search='.htmlspecialchars($searchtext)); } exit(); } ?> <!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>Search</title> </head> <body> <form name="search" method="post" action=""> <label for="search">Local Business Search</label> <input id="searchtext" type="text" name="searchtext"> <input id="submit" type="submit" name="submit" value="Search"> </form> </body> </html>
November 12, 200916 yr Author First of all - thank you! It doesn't throw out an internal server error, but redirection still fails... I can still get to the search results by adding this line though <p>If automatic redirection fails, <a href="business.asp?search=<?php echo $searchtext; ?>">click here to see your search results</a>.</p> What I don't understand is why I get a 500 internal error page with my script, but not with yours - yet neither of them successfully redirects. This script works perfectly on Linux... Also, if you could explain the syntax ? $_POST['submit'] : FALSE; I understand this far $submit = (isset($_POST['submit'])) and why you have used full urls for the redirection? Thanks again for your help - would be nice to get it redirecting too if you have any other suggestions?
November 12, 200916 yr First of all - thank you! It doesn't throw out an internal server error, but redirection still fails... I can still get to the search results by adding this line though <p>If automatic redirection fails, <a href="business.asp?search=<?php echo $searchtext; ?>">click here to see your search results</a>.</p> What I don't understand is why I get a 500 internal error page with my script, but not with yours - yet neither of them successfully redirects. This script works perfectly on Linux... Also, if you could explain the syntax ? $_POST['submit'] : FALSE; I understand this far $submit = (isset($_POST['submit'])) and why you have used full urls for the redirection? Thanks again for your help - would be nice to get it redirecting too if you have any other suggestions? First of all IIS is configured to display a 500 internal error page when a script errors. It's a good idea for production servers (although i'd still recommend a custom error page.) I'm using full URL's as sometimes dependant on your browser, html rendering engine (i.e. html/1.1) relative redirects might not work. I've also added a clause to check if $admin exists and if it doesn't the script sets it to FALSE. The script may be still outputting errors, if so you need to check a few things. Are you starting any sessions before hand? Is there any output above this script? Are you including any file with a terminating ?> - If so remove the ?> as any whitespace after it will be sent to the browser (things like utf-8 bom will be too) The (condition) ? (if true) : (if false); functions like an IF statement, see: http://php.net/manual/en/language.operators.comparison.php "Ternary." EDIT: Come to think of it you need to urlencode the variable you're passing. See code below. <?php // Show all errors and work in development like this.. always. error_reporting(E_ALL); $submit = (isset($_POST['submit'])) ? $_POST['submit'] : FALSE; $searchtext = (isset($_POST['searchtext'])) ? $_POST['searchtext'] : FALSE; $admin = (isset($admin)) ? $admin : FALSE; // Full url $uri = @rtrim(dirname($_SERVER['PHP_SELF']), '/\\'); $host = @$_SERVER['HTTP_HOST']; if($submit) { if ($admin) { header('Location: http://'.$host . $uri.'/business.all.asp?search='.urlencode($searchtext)); } else { header('Location: http://'.$host . $uri.'/business.asp?search='.urlencode($searchtext)); } exit(); } ?> <!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>Search</title> </head> <body> <form name="search" method="post" action=""> <label for="search">Local Business Search</label> <input id="searchtext" type="text" name="searchtext"> <input id="submit" type="submit" name="submit" value="Search"> </form> </body> </html>
November 12, 200916 yr Author Thanks again for your help. I understand that syntax now - and I remember 'learning' it a long time ago... I swapped out the htmlspecialchars for urlencode but I'm still not getting a redirect. I've also tried hard coding the absolute URL for the redirect instead of $uri.$host etc and that also doesn't work The answer to all of the following is no - this is the only php script in an otherwise asp website. The script may be still outputting errors, if so you need to check a few things. Are you starting any sessions before hand? Is there any output above this script? Are you including any file with a terminating ?> - If so remove the ?> as any whitespace after it will be sent to the browser (things like utf-8 bom will be too) Thanks again for your help - any other suggestions? There's no error outputting on the page - I simply see my 'if automatic redirection fails' link, which when clicked takes me to the search results page.
November 12, 200916 yr Thanks again for your help. I understand that syntax now - and I remember 'learning' it a long time ago... I swapped out the htmlspecialchars for urlencode but I'm still not getting a redirect. I've also tried hard coding the absolute URL for the redirect instead of $uri.$host etc and that also doesn't work The answer to all of the following is no - this is the only php script in an otherwise asp website. Theres more in the changed file i presented you with - It's the $admin variable you need to make sure it's defined. just put $admin = TRUE; at the top or something.
November 12, 200916 yr Author Ahhh - I missed that - I'll try that tomorrow when I get back into work, and post back. Thanks for all your help so far
November 13, 200916 yr Author I got it working! And I am such an arse! A combination of exit() and paying attention to damned capitalisation of post data has led to the creation of a working page. Thanks very much for all your help Kieran - very much appreciated. In the end, I realised that the admin variable will never be passed to this script, so I only needed this pared down version: <?php // Show all errors and work in development like this.. always. error_reporting(E_ALL); $submit = (isset($_POST['Submit'])) ? $_POST['Submit'] : FALSE; $searchtext = (isset($_POST['searchtext'])) ? $_POST['searchtext'] : FALSE; if($submit) { header('Location: http://full_url_here/business.asp?search='.urlencode($searchtext)); exit(); } else {echo "<p>Submit not reading</p>";} ?>
November 13, 200916 yr I got it working! And I am such an arse! A combination of exit() and paying attention to damned capitalisation of post data has led to the creation of a working page. Thanks very much for all your help Kieran - very much appreciated. In the end, I realised that the admin variable will never be passed to this script, so I only needed this pared down version: <?php // Show all errors and work in development like this.. always. error_reporting(E_ALL); $submit = (isset($_POST['Submit'])) ? $_POST['Submit'] : FALSE; $searchtext = (isset($_POST['searchtext'])) ? $_POST['searchtext'] : FALSE; if($submit) { header('Location: http://full_url_here/business.asp?search='.urlencode($searchtext)); exit(); } else {echo "<p>Submit not reading</p>";} ?> re: capitalisation of post vars i should have mentioned it - if you noticed in examples i posted i also included the html
Create an account or sign in to comment