Link to home
Start Free TrialLog in
Avatar of helomedic
helomedic

asked on

PHP search form not working

I have a search page created with php that worked great until a couple days ago. I think the server may have been upgraded and something in my code is deprecated, because all I see now is a blank white screen without any error  messages. Can somebody please tell me what is wrong with the code? Here is the page code in a file called search.php. Any help is appreciated!

<?PHP
// Configuration Variables
$my_server = "http://".getenv("SERVER_NAME").":".getenv("SERVER_PORT"); // This identifies the server where the search takes place (The default will usually work in most situations)
$my_root = getenv("DOCUMENT_ROOT"); // Identifies the document root (This default will usually work)
$s_dirs = array("/","/tests/","/docs/") This is the list of directories that will be searched.  It is a comma separated list that is stored in an array.
$s_skip = array("..","."); // This is the list of directories that will be skipped during the search.  It is another comma separated list.  The default values of ".." and "." should always be listed.
$s_files = "html|htm|HTM|HTML"; // The extensions of the files that will be searched.
$min_chars = "3"; // The minimum number of characters that must be entered to perform the search
$max_chars = "50"; // The maximum number of characters that can be submited to perform the search
$default_val = "Enter Text Search Here"; // This is the default text that will appear in the seach box.
$limit_hits = array("5","10","25","50","100"); // How many hits should be displayed, to suppress the select-menu simply use one value in the array --> array("100")

//The following are the messages that will be displayed to the user.
$message_1 = "Invalid Search Term!"; 
$message_2 = "Please enter at least '$min_chars', and at most '$max_chars' characters."; // The seach term was either too long or too short.
$message_3 = "Your Search Result for:"; // Heading
$message_4 = "Sorry, no matches found."; 
$message_5 = "results"; 
$message_6 = "Match case"; 
$no_title = "Untitled Page"; // This will be displayed if no title or empty title is found in file
$limit_extracts_extracts = ""; // How many text excerpts per file do you like to display. Default: "" --> every extract, alternative: 'integer' e.g. "3"
$byte_size = "512000"; // How many bytes per file should be searched? Reduce to increase speed



function search_form($_GET, $limit_hits, $default_val, $message_5, $message_6, $PHP_SELF) {
	@$keyword=$_GET['keyword'];
	@$case=$_GET['case'];
	@$limit=$_GET['limit'];
	echo "<h3>Search Textbook</h3>";
	echo
	"<form action=\"$PHP_SELF\" method=\"GET\">\n",
	"<input type=\"hidden\" value=\"SEARCH\" name=\"action\">\n",
	"<input type=\"text\" name=\"keyword\" class=\"darkbox\" size=\"25\"  maxlength=\"50\" value=\"";
	if(!$keyword)
		echo "$default_val";
	else
		echo str_replace("&amp;","&",htmlentities($keyword));
	echo "\" ";
	echo "onFocus=\" if (value == '";
	if(!$keyword)
		echo "$default_val"; 
	else
		echo str_replace("&amp;","&",htmlentities($keyword));
	echo "') {value=''}\" onBlur=\"if (value == '') {value='";
	if(!$keyword)
		echo "$default_val"; 
	else
		echo str_replace("&amp;","&",htmlentities($keyword));
	echo "'}\"> ";
	$j=count($limit_hits);
	if($j==1)
		echo "<input type=\"hidden\" value=\"".$limit_hits[0]."\" name=\"limit\">";
	elseif($j>1) {
		echo
		"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<select name=\"limit\" class=\"select\">\n";
		for($i=0;$i<$j;$i++) {
			echo "<option value=\"".$limit_hits[$i]."\"";
			if($limit==$limit_hits[$i])
				echo "SELECTED";
			echo ">".$limit_hits[$i]." $message_5</option>\n";
			}
		echo "</select> ";
		}
	echo
	"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type=\"submit\" value=\"Search\" class='darkbox'>\n",
	"<br>\n",
	"<br>\n",
	"<span class=\"darkbox\">$message_6</span>&nbsp;&nbsp; <input type=\"checkbox\" name=\"case\" value=\"true\" class=\"darkbox\"";
	if($case)
		echo " CHECKED";
	}

function search_headline($_GET, $message_3) {
	@$keyword=$_GET['keyword'];
	@$action=$_GET['action'];
	if($action == "SEARCH") 
		echo "<h4 class=\"result\">$message_3 '<i>".htmlentities(stripslashes($keyword))."</i>'</h4>";
	}

function search_error($_GET, $min_chars, $max_chars, $message_1, $message_2, $limit_hits) {
	global $_GET;
	@$keyword=$_GET['keyword'];
	@$action=$_GET['action'];
	@$limit=$_GET['limit'];
	if($action == "SEARCH") { 
		if(strlen($keyword)<$min_chars||strlen($keyword)>$max_chars||!in_array ($limit, $limit_hits)) { 
			echo "<p class=\"result\"><b>$message_1</b><br>$message_2</p>";
			$_GET['action'] = "ERROR";
			}
		}
	}

function search_dir($my_server, $my_root, $s_dirs, $s_files, $s_skip, $message_1, $message_2, $no_title, $limit_extracts, $byte_size, $_GET) {
	global $count_hits;
	@$keyword=$_GET['keyword'];
	@$action=$_GET['action'];
	@$limit=$_GET['limit'];
	@$case=$_GET['case'];
	if($action == "SEARCH") { 
		foreach($s_dirs as $dir) { 
			$handle = @opendir($my_root.$dir);
			while($file = @readdir($handle)) {
				if(in_array($file, $s_skip)) { 
					continue;
					}
				elseif($count_hits>=$limit) {
					break; 
					}
				elseif(is_dir($my_root.$dir."/".$file)) { 
					$s_dirs = array("$dir/$file");
					search_dir($my_server, $my_root, $s_dirs, $s_files, $s_skip, $message_1, $message_2, $no_title, $limit_extracts, $byte_size, $_GET); 
					}
				elseif(preg_match("/($s_files)$/i", $file)) { 
					$fd=fopen($my_root.$dir."/".$file,"r");
					$text=fread($fd, $byte_size); // 500 KB
					$keyword_html = htmlentities($keyword);
					if($case) { 
						$do=strstr($text, $keyword)||strstr($text, $keyword_html);
						}
					else {
						$do=stristr($text, $keyword)||stristr($text, $keyword_html);
						}
					if($do)	{
						$count_hits++; 
						if(preg_match_all("=<title[^>]*>(.*)</title>=siU", $text, $titel)) { 
							if(!$titel[1][0]) 
								$link_title=$no_title; 
							else
								$link_title=$titel[1][0];  
							}
						else {
							$link_title=$no_title; 
							}
						echo "<a href=\"$my_server$dir/$file\" target=\"_self\" class=\"result\">$count_hits.  $link_title</a><br>"; 
						$auszug = strip_tags($text);
						$keyword = preg_quote($keyword); 
						$keyword = str_replace("/","\/","$keyword");
						$keyword_html = preg_quote($keyword_html); 
						$keyword_html = str_replace("/","\/","$keyword_html");
						echo "<span class=\"extract\">";
						if(preg_match_all("/((\s\S*){0,8})($keyword|$keyword_html)((\s?\S*){0,10})/i", $auszug, $match, PREG_SET_ORDER)); {
							if(!$limit_extracts)
								$number=count($match);
							else
								$number=$limit_extracts;
							for ($h=0;$h<$number;$h++) { 
								if (!empty($match[$h][3]))
									printf("<i><b>..</b> %s<b>%s</b>%s <b>..</b></i>", $match[$h][1], $match[$h][3], $match[$h][4]);
								}
							}
						echo "</span><br><br>";
						flush();
						}
					fclose($fd);
					}
				}
	  		@closedir($handle);
			}
		}
	}

function search_no_hits($_GET, $count_hits, $message_4) {
	@$action=$_GET['action'];
	if($action == "SEARCH" && $count_hits<1) 
		echo "<p class=\"result\">$message_4</p>";
	}
?>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
	<title>Search</title>
    <link href="styles/test.css" rel="stylesheet" type="text/css">
</head>
<body>
<table border="0" align="center" cellpadding="1" cellspacing="10" class="box">
  <tr align="left" valign="top">
	<td width="700" class="darkbox"><table width="500" border="0" align="center" cellpadding="10" cellspacing="0">
      <tr align="left" valign="top">
        <td class="darkbox"><br /><?PHP
    search_form($_GET, $limit_hits, $default_val, $message_5, $message_6, $PHP_SELF);
?>   
<br />
<?PHP
   echo "<br /><br /><hr align='center' width='75%' color='#000000' size='2'>";
   search_headline($_GET, $message_3);
   search_error($_GET, $min_chars, $max_chars, $message_1, $message_2, $limit_hits);
   search_dir($my_server, $my_root, $s_dirs, $s_files, $s_skip, $message_1, $message_2, $no_title, $limit_extracts, $byte_size, $_GET);
   search_no_hits($_GET, $count_hits, $message_4);
?>     </td>
      </tr>
    </table></td>
  </tr>
</table>

</body>
</html>

Open in new window

Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

What version of PHP were you running before?  What version are you running now?  Once we know that we can show you the changelog pages for the upgrade.  That will probably give us a strong hint about what might be going on here.

Also, any run-time error may cause the white screen of death.  You may want to check and see if there is an error log on the server.  Often a well-configured server will log the errors, even if it does not show them on the screen.
This line near the top contains a parse error.

$s_dirs = array("/","/tests/","/docs/") This is the list of directories that will be searched.  It is a comma separated list that is stored in an array.

Are you sure you posted the correct script?  No PHP installation anywhere would ever have been able to run this because of the parse error in that line.
Avatar of 10023
10023

missing a semicolon at   $s_dirs = array("/","/tests/","/docs/")
Avatar of helomedic

ASKER

Sorry, there was a semicolon ,but I had changed the directories for this post and for got to add it. I sue this form on about 100 different folders, so here is the one I changed complete -
 $s_dirs = array("/houk_anthro","/houk_anthro/ch1","/houk_anthro/ch2","/houk_anthro/ch3","/houk_anthro/ch4","/houk_anthro/ch5","/houk_anthro/ch6","/houk_anthro/ch7","/houk_anthro/ch8","/houk_anthro/ch9","/houk_anthro/ch10","/houk_anthro/ch11","/houk_anthro/ch12","/houk_anthro/ch13","/houk_anthro/ch14","/houk_anthro/ch15");
To test it you just need to change the folders to ones that you have on your testing server.

I believe the server was running php 5.3, but I am not sure what they did, perhaps upgrade to 5.6. Last week they did something and all my php files stopped working. The more recent ones written in php5 were fixed after I files a trouble report, but this was an older script. I don't know what they did and they won't tell me, so I have to kind of figure it out.
Unfortunately, they also changed the control panel credentials, so I am waiting for the correct ones to access the account from my client and see what version of php they are running. I have asked them for the error logs and they won't give them up. The hosting company has been a nightmare and we are moving the site soon, but I need to get this fixed asap.
When I fix the parse error, I get this message:

Fatal error: Cannot re-assign auto-global variable _GET in /home/iconoun/public_html/demo/temp_helomedic.php on line 26

The line in error seems to be this one:
function search_form($_GET, $limit_hits, $default_val, $message_5, $message_6, $PHP_SELF) {

Never saw this error before because $_GET is a "superglobal" variable.  It cannot be mocked (although it can be overwritten) and it is present and addressable in every scope and namespace.  It does not belong in a function definition.
ASKER CERTIFIED SOLUTION
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thank you! Removing the $_GET did the trick - I will use the $null if it acts up, but just taking it out fixed it. I changed the 'PHP_SELF' to $_SERVER['PHP_SELF'] and it broke again, but it works great if I leave it the way it was. I think the $limit_extracts was defined earlier in the script as $limit_extracts_extracts, but I change it to just $limit_extracts and it is all working now, I think. I don't get the errors, so it is hard for me to see what is going on. Again, thank you so much!!
Very fast, had the answer right away. Saved the day!
Thanks for the points - glad we got it working.

Be careful about just "removing" the $_GET from the function definition statement.  The input variables are positional elements.  Here is an example that illustrates the risk, and shows why I used $null as a substitute.  The default error_reporting() in PHP suppresses Notice messages, so you may not get any message about an error in the function, but you have a fairly great risk of incorrect output.  Two identical functions are shown here, called exactly the same.  But in the second one, I've removed one of the function arguments instead of setting it to something like $null.
http://iconoun.com/demo/temp_helomedic.php

<?php

/**
 * See http://www.experts-exchange.com/Programming/Languages/Scripting/PHP/Q_28622102.html
 * PHP function definitions are positional elements
 */

// SET THE DEFAULT ERROR REPORTING TO SUPPRESS NOTICE MESSAGES
error_reporting(E_ALL ^ E_NOTICE);

function summ_1($a, $b, $c)
{
    echo $a + $b + $c;
}

function summ_2($b, $c)
{
    echo $a + $b + $c;
}

$a = 1;
$b = 2;
$c = 3;

summ_1($a, $b, $c);
echo '<br>';
summ_2($a, $b, $c);

Open in new window

Thanks, I set it to $null as you suggest. Thanks again!
10-4, and best of luck with it! ~Ray