Solved

PHP search form not working

Posted on 2015-02-22
14
76 Views
Last Modified: 2015-02-22
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

0
Comment
Question by:helomedic
  • 7
  • 6
14 Comments
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 40624394
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.
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 40624399
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.
0
 
LVL 10

Expert Comment

by:10023
ID: 40624400
missing a semicolon at   $s_dirs = array("/","/tests/","/docs/")
0
 

Author Comment

by:helomedic
ID: 40624402
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");
0
 

Author Comment

by:helomedic
ID: 40624403
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.
0
 

Author Comment

by:helomedic
ID: 40624406
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.
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 40624412
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.
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 108

Expert Comment

by:Ray Paseur
ID: 40624414
0
 
LVL 108

Accepted Solution

by:
Ray Paseur earned 500 total points
ID: 40624419
To get past that problem, I changed all of the function definitions that had $_GET so that the same placeholder was $null.  There is no special meaning to $null, I just made that up.  Once the fatal errors are gone, there is this:  Notice: Undefined variable: PHP_SELF.  Suggest you change that to $_SERVER['PHP_SELF'].

There is also this: Notice: Undefined variable: limit_extracts in /path/to/temp_helomedic.php on line 189

There may be other undefined variables in the script, too.  From the look of the code, this would be a good candidate for refactoring.
0
 

Author Comment

by:helomedic
ID: 40624431
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!!
0
 

Author Closing Comment

by:helomedic
ID: 40624434
Very fast, had the answer right away. Saved the day!
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 40624444
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

0
 

Author Comment

by:helomedic
ID: 40624457
Thanks, I set it to $null as you suggest. Thanks again!
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 40624474
10-4, and best of luck with it! ~Ray
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Consider the following scenario: You are working on a website and make something great - something that lets the server work with information submitted by your users. This could be anything, from a simple guestbook to a e-Money solution. But what…
Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now