young_coder Posted February 18, 2010 Share Posted February 18, 2010 Hey guys! I have this little script saved like index.php <?php session_start(); $_SESSION['raw_query'] = $_GET['q']; $_SESSION['ad_group'] = ucwords(str_replace("-", " ", $_GET['q'])); echo $_SESSION['ad_group']; ?> and I have file pages.txt with name of every page like this: about us contact us site map It is possible when URLs "./index.php?q=about-us", "./index.php?q=contact-us", "./index.php?q=about-us" and so on are required by browser than script to go and check from pages.txt is there line with "about us", "contact us", "site map" etc, and if page is not in list to show error 404 page? Would appreciate some help with this one and thank you advance Cheers! Quote Link to comment Share on other sites More sharing options...
Omirion Posted February 18, 2010 Share Posted February 18, 2010 I don't have time to write out the code but here is my input in theory. Take the _GET input and put it into a var. Do a for each loop on each line of the text file and compare it against the var. On success set a trigger var to 1 On failure the trigger var will remain 0 At the end of the code check if trigger var is 1 if so do whatever. if not goto a predefined 404 page. If you didn't understand or you need help with the code,leave a comment and i'l write it out when i get back in a few hours. Quote Link to comment Share on other sites More sharing options...
teamatomic Posted February 18, 2010 Share Posted February 18, 2010 $str=$_SESSION['ad_group']; $lines=file('./pages.txt'); $found='no'; foreach($lines as $line) { $line=trim("$line"); if ($line == $str){$found='yes';} } if ($found == 'no') { include("./404.php");//* exit; } *or something like that depending on how your controller page is set up to display content. HTH Teamatomic Quote Link to comment Share on other sites More sharing options...
young_coder Posted February 18, 2010 Author Share Posted February 18, 2010 I'm newbie with PHP and I am very thankful on all help with this… I have made and put one 404.php file in same folder with this script, but now that file is shown every time, even if that page is listed in pages.txt Quote Link to comment Share on other sites More sharing options...
teamatomic Posted February 18, 2010 Share Posted February 18, 2010 Showing the code might help a bit. HTH Teamatomic Quote Link to comment Share on other sites More sharing options...
young_coder Posted February 18, 2010 Author Share Posted February 18, 2010 One more time thank you on help Code is very simple and its just this <?php session_start(); $_SESSION['raw_query'] = $_GET['q']; $_SESSION['ad_group'] = ucwords(str_replace("-", " ", $_GET['q'])); $str=$_SESSION['ad_group']; $lines=file('./pages.txt'); $found='no'; foreach($lines as $line) { $line=trim("$line"); if ($line == $str){$found='yes';} } if ($found == 'no') { include("./404.php"); exit; } ?> Quote Link to comment Share on other sites More sharing options...
sader Posted February 18, 2010 Share Posted February 18, 2010 u can try if ($line == $str) { $found='yes'; end($lines); } Quote Link to comment Share on other sites More sharing options...
young_coder Posted February 18, 2010 Author Share Posted February 18, 2010 If I put like this, than is no error 404 <?php session_start(); $_SESSION['raw_query'] = $_GET['q']; $_SESSION['ad_group'] = ucwords(str_replace("-", " ", $_GET['q'])); $str=$_SESSION['ad_group']; $lines=file('./pages.txt'); $found='no'; foreach($lines as $line) { $line=trim("$line"); if ($line == $str){$found='yes';} } if ($line == $str) { $found='yes'; end($lines); } ?> Quote Link to comment Share on other sites More sharing options...
teamatomic Posted February 18, 2010 Share Posted February 18, 2010 The way you are set up and from the code you displayed in your first post you must call your index page like this: index.php?q=about-us are you? if not you must set up a conditional(if statement) to check for the presence of $_GET[q] or it will always expect it and so then without it will not match something form the pages.txt file. HTH Teamatomic Quote Link to comment Share on other sites More sharing options...
young_coder Posted February 18, 2010 Author Share Posted February 18, 2010 Yes, URL look like this h**p://localhost/001/index.php?q=about-us Now in attachment there are two zip files with two different ways to this be done but any of them doesn’t work in this moment [attachment deleted by admin] Quote Link to comment Share on other sites More sharing options...
teamatomic Posted February 18, 2010 Share Posted February 18, 2010 Just noticed something. Either get rid of ucwords or cap the entries in the pages.txt file. As far as the conditional is concerned, (About Us != about us) HTH Teamatomic Quote Link to comment Share on other sites More sharing options...
young_coder Posted February 18, 2010 Author Share Posted February 18, 2010 "...Either get rid of ucwords or cap the entries in the pages.txt file...." Yeah... That was problem… 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.