re: sending results to a new page

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

cembert
Forum Newbie
Posts: 11
Joined: Tue Jul 03, 2007 11:29 am

re: sending results to a new page

Post by cembert »

Everah | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


Attempting to get the code below to show results in a new page (ie: results.php) 
I am having some difficulty on how to tackle this task.

I believe PHP_SELF has to be adjusted, but have had really little success.

search_form($HTTP_GET_VARS, $limit_hits, $default_val, $message_5, $message_6, $PHP_SELF);

any help would be appreciated to send me in the right direction.




original code:

Code: Select all

<?

$my_server = "http://".getenv("SERVER_NAME").":".getenv("SERVER_PORT"); $my_root = getenv("DOCUMENT_ROOT"); $s_dirs = array(""); 
$s_skip = array("..",".","subdir2"); 
$s_files = "html|htm|HTM|HTML|php3|php4|php|txt"; 
$min_chars = "3"; 
$max_chars = "30"; 
$default_val = "Searchphrase"; 
$limit_hits = array("5","10","25","50","100"); 
$message_1 = "Invalid Searchterm!";
$message_2 = "Please enter at least '$min_chars', highest '$max_chars' characters."; 
$message_3= "Your searchresult for:"; 
$message_4 = "Sorry, no hits.";
$message_5 = "results";
$message_6 = "Match case"; 
$no_title = "Untitled"; 
$limit_extracts_extracts = ""; 
$byte_size = "51200"; 


function search_form($HTTP_GET_VARS, $limit_hits, $default_val, $message_5, $message_6, $PHP_SELF) {
	@$keyword=$HTTP_GET_VARS['keyword'];
	@$case=$HTTP_GET_VARS['case'];
	@$limit=$HTTP_GET_VARS['limit'];
	echo
	"<form action=\"$PHP_SELF\" method=\"GET\">\n",
	"<input type=\"hidden\" value=\"SEARCH\" name=\"action\">\n",
	"<input type=\"text\" name=\"keyword\" class=\"text\" size=\"10\"  maxlength=\"30\" value=\"";
	if(!$keyword)
		echo "$default_val";
	else
		echo str_replace("&","&",htmlentities($keyword));
	echo "\" ";
	echo "onFocus=\" if (value == '";
	if(!$keyword)
		echo "$default_val"; 
	else
		echo str_replace("&","&",htmlentities($keyword));
	echo "') {value=''}\" onBlur=\"if (value == '') {value='";
	if(!$keyword)
		echo "$default_val"; 
	else
		echo str_replace("&","&",htmlentities($keyword));
	echo "'}\"> ";
	$j=count($limit_hits);
	if($j==1)
		echo "<input type=\"hidden\" value=\"".$limit_hits[0]."\" name=\"limit\">";
	elseif($j>1) {
		echo
		"<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
	"<input type=\"submit\" value=\"OK\" class=\"button\">\n",
	"<br>\n",
	"<span class=\"checkbox\">$message_6</span> <input type=\"checkbox\" name=\"case\" value=\"true\" class=\"checkbox\"";
	if($case)
		echo " CHECKED";
	echo
	">\n",
	"<br>\n",
	"<a href=\"http://www.terraserver.de/\" class=\"ts\" target=\"_blank\">Powered by terraserver.de/search</a>",
	"</form>\n";
	}


// search_headline(): Ueberschrift Suchergebnisse
function search_headline($HTTP_GET_VARS, $message_3) {
	@$keyword=$HTTP_GET_VARS['keyword'];
	@$action=$HTTP_GET_VARS['action'];
	if($action == "SEARCH") // Volltextsuche
		echo "<h1 class=\"result\">$message_3 '<i>".htmlentities(stripslashes($keyword))."</i>'</h1>";
	}



function search_error($HTTP_GET_VARS, $min_chars, $max_chars, $message_1, $message_2, $limit_hits) {
	global $HTTP_GET_VARS;
	@$keyword=$HTTP_GET_VARS['keyword'];
	@$action=$HTTP_GET_VARS['action'];
	@$limit=$HTTP_GET_VARS['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>";
			$HTTP_GET_VARS['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, $HTTP_GET_VARS) {
	global $count_hits;
	@$keyword=$HTTP_GET_VARS['keyword'];
	@$action=$HTTP_GET_VARS['action'];
	@$limit=$HTTP_GET_VARS['limit'];
	@$case=$HTTP_GET_VARS['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, $HTTP_GET_VARS); 
					}
				elseif(preg_match("/($s_files)$/i", $file)) {
					$fd=fopen($my_root.$dir."/".$file,"r");
					$text=fread($fd, $byte_size); 
					$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,3})($keyword|$keyword_html)((\s?\S*){0,3})/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($HTTP_GET_VARS, $count_hits, $message_4) {
	@$action=$HTTP_GET_VARS['action'];
	if($action == "SEARCH" && $count_hits<1) 
		echo "<p class=\"result\">$message_4</p>";
	}

?>

Code: Select all

<?
search_form($HTTP_GET_VARS, $limit_hits, $default_val, $message_5, $message_6, $PHP_SELF);
?>

Code: Select all

<?
search_headline($HTTP_GET_VARS, $message_3);
search_error($HTTP_GET_VARS, $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, $HTTP_GET_VARS);
search_no_hits($HTTP_GET_VARS, $count_hits, $message_4);
?>

Everah | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
cembert
Forum Newbie
Posts: 11
Joined: Tue Jul 03, 2007 11:29 am

Post by cembert »

will do. Thanks.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Why are you using long superglobal arrays and why so much use of error suppression?
cembert
Forum Newbie
Posts: 11
Joined: Tue Jul 03, 2007 11:29 am

Post by cembert »

The script is a basic flat file data base search engine.
Created originally by Holger Eichert at Terraserver.

I realize alot of the scripting is somewhat out of date.
My initial thoughts were to redirect the results and then take a look at some other updates / Mods
cembert
Forum Newbie
Posts: 11
Joined: Tue Jul 03, 2007 11:29 am

Post by cembert »

I have tested the original script on a linux server and it runs fine.
The only issue is that the php results load in the same page (search.php)

I was replacing $HTTP_GET_VARS and $PHP_SELF and using an IFrame, with little success.
($HTTP_GET_VARS, $limit_hits, $default_val, $message_5, $message_6, $PHP_SELF);
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

So instead of a post back you want a new page? Is that it?

If so, change the form action attribute to a new page name (say "results.php") and in that page, take the code used to display the results in "searcg.php" and paste that into "results.php".
cembert
Forum Newbie
Posts: 11
Joined: Tue Jul 03, 2007 11:29 am

Post by cembert »

Followed your concept.
I get a Fatal error: Call to undefined function: search_form() in in the results.php on line 7
What happens with the $HTTP_GET_VARS and the $PHP_SELF


Code: Select all

<table border="0" cellspacing="1" cellpadding="0" bgcolor="#03629C">
  <tr align="left" valign="top">
    <td><table border="0" cellspacing="0" cellpadding="3" bgcolor="#FFFFFF">
      <tr align="left" valign="top">
        <td>
<?
search_form($HTTP_GET_VARS, $limit_hits, $default_val, $message_5, $message_6, $PHP_SELF);
?>
        </td>
      </tr>
    </table></td>
  </tr>
</table>
<?
search_headline($HTTP_GET_VARS, $message_3);
search_error($HTTP_GET_VARS, $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, $HTTP_GET_VARS);
search_no_hits($HTTP_GET_VARS, $count_hits, $message_4);
?>
cembert
Forum Newbie
Posts: 11
Joined: Tue Jul 03, 2007 11:29 am

Post by cembert »

The following script takes me to the results.php

"<form action=\"results.php\" method=\"GET\">\n",

I now need to show the results.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

It means you are missing an include more than likely. Look closely at the code for the original stuff you posted. If there are any included files, included those in result.php also.

As for the HTTP_GET_VARS, those are coming from the query string, so the application is looking for things past in the URL, like page.php?get_var1=this&get_vart2=that
cembert
Forum Newbie
Posts: 11
Joined: Tue Jul 03, 2007 11:29 am

Post by cembert »

include 'http://www.something.com/file.php';
I did not see any type of include (ie: sample above)

My toughts are that it may have something to do with $PHP_SELF
-C
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

cembert wrote:include 'http://www.something.com/file.php';
I did not see any type of include (ie: sample above)

My toughts are that it may have something to do with $PHP_SELF
-C
Include statement with protocol = bad (PHP assumes it's remote).
PHP_SELF = insecure (when left unfiltered).
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

$PHP_SELF is an insecure deprecated global variable that should not be used. But it has nothing to do with your problem. Your problem lies, unfortunately, in the fact that all of the user defined functions are coded into the search.php page instead of being included as a utility file. It is easy to fix, but before I tell you how to do it I am going to let you try to figure it out on your own first.
cembert
Forum Newbie
Posts: 11
Joined: Tue Jul 03, 2007 11:29 am

Post by cembert »

I'll work on it this evening.....get back to you tommorrow.
Thanks for your help.
-Chris
cembert
Forum Newbie
Posts: 11
Joined: Tue Jul 03, 2007 11:29 am

Post by cembert »

So far I have broken search.php into the following files

return.php
main.css
config.php
search_form.php
search_headline.php
search_error.php
search_dir.php
search_no_hits.php

Opening config to begin with, brings up nothing.
I am getting Call to undefined function errors for the rest.
This leads me to wonder how am I going to connect all of necessary files.
Looking back at the post's there have to be some key includes.

I'll continue to research.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Wow, that is a hodgeodge of some funk nasty code. Anyway, I cleaned it up a little bit. I changed one thine... the reference to $PHP_SELF in the search_form function. Instead of giving it $PHP_SELF, you can leave it blank or pass the name of the file you want to push the form to (in your case, 'results.php').

This file contains your functions and the settings established at the top of your script:
search-functions.php

Code: Select all

<?php
/**
 * These are some of the default values used throughout the script
 */
$my_server = "http://".getenv("SERVER_NAME").":".getenv("SERVER_PORT"); 
$my_root = getenv("DOCUMENT_ROOT"); 
$s_dirs = array("");
$s_skip = array("..",".","subdir2");
$s_files = "html|htm|HTM|HTML|php3|php4|php|txt";
$min_chars = 3; // Integers do not need quotes
$max_chars = 30; // Integer... no quotes needed
$default_val = "Searchphrase";
$limit_hits = array(5,10,25,50,100);
$message_1 = "Invalid Searchterm!";
$message_2 = "Please enter at least '$min_chars', highest '$max_chars' characters.";
$message_3= "Your searchresult for:";
$message_4 = "Sorry, no hits.";
$message_5 = "results";
$message_6 = "Match case";
$no_title = "Untitled";
$limit_extracts_extracts = "";
$byte_size = 51200; // This is an int and should not have quotes

/**
 * These are utility functions used in the script
 * All functions should be kept in their own files for use when you need them
 */
/**
 * $PHP_SELF REMOVED IN EXCHANGE FOR MORE EXTENSIBLE $form_action
 */
function search_form($HTTP_GET_VARS, $limit_hits, $default_val, $message_5, $message_6, $form_action) {
        $keyword = isset($HTTP_GET_VARS['keyword']) ? $HTTP_GET_VARS['keyword'] : ''; // These are ternaries to check if the values ares set or not
        $case = isset($HTTP_GET_VARS['case']) ? $HTTP_GET_VARS['case'] : '';
        $limit = isset($HTTP_GET_VARS['limit']) ? $HTTP_GET_VARS['limit'] : '';
        
        // I am relly opposed to output from a function
        // But this is not my code, I am just cleaning it 
        if (empty($form_action)) {
            $form_action = basename(__FILE__); // Similar to PHP_SELF, but a lot safer
        }
        
        // Also noticed the same repeated, so we handle that here
        if (empty($keyword)) {
                $form_val =  $default_val;
        } else {
                $form_val = str_replace("&","&",htmlentities($keyword));
        }
        
        echo '<form action="' . $form_action . '" method="GET">
        <input type="hidden" value="SEARCH" name="action">
        <input type="text" name="keyword" class="text" size="10"  maxlength="30" value="' . $form_val . '" onFocus=" if (value == \'' . $form_val . '\') {value=\'\'}" onBlur="if (value == \'\') {value=\'' . $form_val . '\'}">';
        
        $j = count($limit_hits);
        if ($j == 1) {
                echo '<input type="hidden" value="' . $limit_hits[0] . '" name="limit">';
        } elseif ($j > 1) {
                echo '<select name="limit" class="select">';
                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>';
                }
                echo '</select>';
        }
        echo '<input type="submit" value="OK" class="button"><br>
        <span class="checkbox">' . $message_6 . '</span>
        <input type="checkbox" name="case" value="true" class="checkbox"';
        if (!empty($case)) {
                echo ' CHECKED';
        }
        echo '>
        <br>
        <a href="http://www.terraserver.de/" class="ts" target="_blank">Powered by terraserver.de/search</a>
        </form>';
}

// search_headline(): Ueberschrift Suchergebnisse
function search_headline($HTTP_GET_VARS, $message_3) {
        $keyword = isset($HTTP_GET_VARS['keyword']) ? $HTTP_GET_VARS['keyword'] : '';
        $action = isset($HTTP_GET_VARS['action']) ? $HTTP_GET_VARS['action'] : '';
        if ($action == "SEARCH") { // Volltextsuche
                echo '<h1 class="result">' . $message_3 . '"<i>' . htmlentities(stripslashes($keyword)) . '</i>"</h1>';
        }
}


function search_error($HTTP_GET_VARS, $min_chars, $max_chars, $message_1, $message_2, $limit_hits) {
        global $HTTP_GET_VARS;
        $keyword = isset($HTTP_GET_VARS['keyword']) ? $HTTP_GET_VARS['keyword'] : '';
        $action = isset($HTTP_GET_VARS['action']) ? $HTTP_GET_VARS['action'] : '';
        $limit = isset($HTTP_GET_VARS['limit']) ? $HTTP_GET_VARS['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>'; 
                        $HTTP_GET_VARS['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, $HTTP_GET_VARS) {
        global $count_hits;
        $keyword = isset($HTTP_GET_VARS['keyword']) ? $HTTP_GET_VARS['keyword'] : '';
        $action = isset($HTTP_GET_VARS['action']) ? $HTTP_GET_VARS['action'] : '';
        $limit = isset($HTTP_GET_VARS['limit']) ? $HTTP_GET_VARS['limit'] : '';
        $case = isset($HTTP_GET_VARS['case']) ? $HTTP_GET_VARS['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, $HTTP_GET_VARS);
                                } elseif (preg_match("/($s_files)$/i", $file)) {
                                        $fd = fopen($my_root.$dir."/".$file,"r");
                                        $text = fread($fd, $byte_size);
                                        $keyword_html = htmlentities($keyword);
                                        if (!empty($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,3})($keyword|$keyword_html)((\s?\S*){0,3})/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($HTTP_GET_VARS, $count_hits, $message_4) {
        $action = isset($HTTP_GET_VARS['action']) ? $HTTP_GET_VARS['action'] : '';
        if ($action == "SEARCH" && $count_hits < 1) {
                echo '<p class="result">' . $message_4 . '</p>';
        }
}
?>
This is the application:
results.php

Code: Select all

<?php
require_once 'search-functions.php';
search_headline($HTTP_GET_VARS, $message_3);
search_error($HTTP_GET_VARS, $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, $HTTP_GET_VARS);
search_no_hits($HTTP_GET_VARS, $count_hits, $message_4);
?>
See if that doesn't help a little bit more.
Post Reply