Jump to content

search facility


Cassius

Recommended Posts

I have a form which has an input field called "year", and once the submit button is clicked, the following code will run.

 

<?php
$userinput = @$_POST["year"];
$path = "index.php?mC=$userinput.php";
Header("Location: $path");
?

 

The above code will display a page inside a div on my main page and this works fine. What i would like to do is to see if i can alter the above code, so that if the page is not found, i will take the user to a not found page withing my index.php page.

 

I know that it has to be done something along the if ... else statement, but not sure which keywords to use.

 

Thank you for any help.

Link to comment
Share on other sites

you're right you would use an if statement to check if the file exists with "file_exists() function", eg:

 

<?php
$userinput = @$_POST["year"];
if(!file_exists($userinput)){
  Header("Location: index.php?mC=notfound.php");
}else{
  $path = "index.php?mC=$userinput.php";
  Header("Location: $path");
}
?>

Link to comment
Share on other sites

Hi,

 

thanks for your reply. However, when i am trying the search feature i am always getting the not found page. For example in the root folder i have a 2007.php file, so when i am typing 2007 in the search field i am getting the not found.

 

I tried altering the code as follows, but still the same problem:

 

<?php
$userinput = @$_POST["year"];
$path = "index.php?mC=$userinput.php";
if(!file_exists($path)){
  Header("Location: index.php?mC=notfound.php");
}else{
  Header("Location: $path");
}
?>

 

Any ideas what i have wrong please?

 

Thank you

Link to comment
Share on other sites

check what your asking php to do:

 

file_exists("index.php?mC=notfound.php"));

 

is impossible will never find such a file.

----

 

are you sure the files exist in the same folder as your original script?

 

could you give a simple directory structure where your including these files from?

 

Link to comment
Share on other sites

Hi,

 

Thanks for your reply.

 

the script which is called processsearch.php and the file 2007.php are both in this folder C:\Inetpub\wwwroot\PostageStamps ... however, in the browser i type http://localhost/postagestamps/index.php to get to the homepage.

 

Also, i am just testing it out, so once i get it to work i am planning to move the 2007.php to a subfolder, so that i don't have a mess in the root folder.

Link to comment
Share on other sites

thanks for the input, im not sure why you keep getting "not found" page....

 

is this code snippet in your index.php file? or a seperate file?

 

if its a seperate file try this and tell me what it says:

 

thanks, ok:

 

<?php
$subfolder = ""; // leave blank if in same folder, if in subfolder remember to add a / to the end

$userinput = @$_POST["year"];

print_r($_REQUEST); // debug line (print all _POST and _GET variables

if(file_exists($subfolder.$userinput)){
  $path = "index.php?mC=$userinput.php";
  Header("Location: $path");
}else{
  Header("Location: index.php?mC=notfound.php");
}
?>

Link to comment
Share on other sites

Hi,

 

The following is the contents of my index file:

 

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<link rel="stylesheet" href="main.css" media="screen" charset="utf-8">
<?php
     if (!isset($_GET['mC'])) {
 $file = "home.php";
 } else {
 $file = $_GET['mC'];
 }
?>
<title>Malta Postage Stamps</title>
</head>

<body>
<div id="grandcontainer">
<div id="headersection"><img src="images/menuimage.jpg" alt="topbanner" width="756px" height="90px"></div>
<div id="menusection">
  <ul class="leftmenumainbullet">
   <li class="leftmenubullet"><a class="leftmenu" href="index.php"><img class="bulletarrow" src="images/bulletarrow.gif" border="0">Home</a></li>
   <li class="leftmenubullet"><a class="leftmenu" href="index.php?mC=stampcatalogue.php"><img class="bulletarrow" src="images/bulletarrow.gif" border="0">Stamp Catalogue</a></li>
   <li class="leftmenubullet"><a class="leftmenu" href="index.php?mC=collectorsads.php"><img class="bulletarrow" src="images/bulletarrow.gif" border="0">Collector's Ads</a></li>
   <li class="leftmenubullet"><a class="leftmenu" href="index.php?mC=references.php"><img class="bulletarrow" src="images/bulletarrow.gif" border="0">References</a></li>
   <li class="leftmenubullet"><a class="leftmenu" href="index.php?mC=articles.php"><img class="bulletarrow" src="images/bulletarrow.gif" border="0">Articles</a></li>
   <li class="leftmenubullet"><a class="leftmenu" href="index.php?mC=glossary.php"><img class="bulletarrow" src="images/bulletarrow.gif" border="0">Glossary</a></li>
   <li class="leftmenubullet"><a class="leftmenu" href="index.php?mC=links.php"><img class="bulletarrow" src="images/bulletarrow.gif" border="0">Links</a></li>
   <li class="leftmenubullet"><a class="leftmenu" href="index.php?mC=faqs.php"><img class="bulletarrow" src="images/bulletarrow.gif" border="0">FAQs</a></li>
   <li class="leftmenubullet"><a class="leftmenu" href="index.php?mC=about.php"><img class="bulletarrow" src="images/bulletarrow.gif" border="0">About</a></li>
   <li class="leftmenubullet"><a class="leftmenu" href="index.php?mC=contact.php"><img class="bulletarrow" src="images/bulletarrow.gif" border="0">Contact</a></li>
   <li class="leftmenubullet"><a class="leftmenu" href="index.php?mC=disclaimer.php"><img class="bulletarrow" src="images/bulletarrow.gif" border="0">Disclaimer</a></li>
  </ul>
  <form method="post" action="processsearch.php">
  <p class="searchheader">Search Catalogue
  <input type="text" name="year" value="Enter a Year">
  <input type="submit" value="Search">
  </form>
  </p>
</div>
<div id="mainsection"><?php include($file);?></div>
<div id="clearfloat"></div>
<div id="footersection"></div>
</div>
</body>
</html>

 

Then the processsearch.php is a file on its own, and the 2007.php file is another file on its own, which should load inside a <div> in the index file.

Link to comment
Share on other sites

try this:

 

processsearch.php

<?php
$subfolder = ""; // leave blank if in same folder, if in subfolder remember to add a / to the end

$userinput = @$_POST["year"];

print_r($_REQUEST); // debug line (print all _POST and _GET variables
exit(); // debug line, stops processing

if(file_exists($subfolder.$userinput)){
  $path = "index.php?mC=$userinput.php";
  Header("Location: $path");
}else{
  Header("Location: index.php?mC=notfound.php");
}
?>

 

tell me what it says

Link to comment
Share on other sites

ok great, my mistake,

 

try this: (sorry lol)

 

<?php
$subfolder = ""; // leave blank if in same folder, if in subfolder remember to add a / to the end

$userinput = @$_POST["year"];

if(file_exists($subfolder.$userinput.".php")){ // changed this line to add .php extension
  $path = "index.php?mC=$userinput.php";
  Header("Location: $path");
}else{
  Header("Location: index.php?mC=notfound.php");
}
?>

Link to comment
Share on other sites

Thank you very much for your time, it worked perfect now :)

 

Just a last thing, as you can see i am putting these pages inside a div on the index page, now if a search engine puts one of the content files up as search result, and people click it, they will get to see the text, but they won't be able to surf on because it isn't shown in the parent page.

 

is there a way to go about this ?

 

Thanks

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.