Cassius Posted February 18, 2008 Share Posted February 18, 2008 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. Quote Link to comment Share on other sites More sharing options...
uniflare Posted February 18, 2008 Share Posted February 18, 2008 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"); } ?> Quote Link to comment Share on other sites More sharing options...
Cassius Posted February 18, 2008 Author Share Posted February 18, 2008 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 Quote Link to comment Share on other sites More sharing options...
uniflare Posted February 18, 2008 Share Posted February 18, 2008 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? Quote Link to comment Share on other sites More sharing options...
Cassius Posted February 18, 2008 Author Share Posted February 18, 2008 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. Quote Link to comment Share on other sites More sharing options...
uniflare Posted February 18, 2008 Share Posted February 18, 2008 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"); } ?> Quote Link to comment Share on other sites More sharing options...
Cassius Posted February 18, 2008 Author Share Posted February 18, 2008 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. Quote Link to comment Share on other sites More sharing options...
uniflare Posted February 18, 2008 Share Posted February 18, 2008 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 Quote Link to comment Share on other sites More sharing options...
Cassius Posted February 18, 2008 Author Share Posted February 18, 2008 when i clicked on the search button, i got a blank page wit Array ( [year] => 2007 ) Quote Link to comment Share on other sites More sharing options...
uniflare Posted February 18, 2008 Share Posted February 18, 2008 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"); } ?> Quote Link to comment Share on other sites More sharing options...
Cassius Posted February 18, 2008 Author Share Posted February 18, 2008 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 Quote Link to comment Share on other sites More sharing options...
uniflare Posted February 18, 2008 Share Posted February 18, 2008 have a look at robots.txt usage on google, i have no experience in this matter however, you will need to limit the crawler to specific webpages or files, and use meta tags and keywords in your index file Quote Link to comment Share on other sites More sharing options...
Cassius Posted February 18, 2008 Author Share Posted February 18, 2008 thank you very much. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.