March 20, 20188 yr Just checking you want to know /folder from the url? You could use ... $current_url = $_SERVER['REQUEST_URI']; That would get anything from the url bar, e.g. /folder/?fish=true etc. If you already know the folder name and you want to make links like /folder/somefile.jpg then you could state ... $save_dir = "/folder"; $image_url = $save_dir . "/somefile.jpg"; ... really depends what you need to know.
March 20, 20188 yr 23 minutes ago, gigman7 said: Set the name of the folder to a variable. Do as @BrowserBugs suggests or use getcwd: http://php.net/manual/en/function.getcwd.php A lot depends on if you are using the folder/directory or just want to refer to it. If you are using something like wordpress there are a whole range of built in functions you can use. For example: https://developer.wordpress.org/reference/functions/get_bloginfo/ Other platforms will have similar.
March 20, 20188 yr Author 34 minutes ago, BrowserBugs said: Just checking you want to know /folder from the url? You could use ... $current_url = $_SERVER['REQUEST_URI']; That would get anything from the url bar, e.g. /folder/?fish=true etc. If you already know the folder name and you want to make links like /folder/somefile.jpg then you could state ... $save_dir = "/folder"; $image_url = $save_dir . "/somefile.jpg"; ... really depends what you need to know. Thanks a lot, that worked. Now how to I get rid of the slash? The stripslashes function isn't working for me.
March 20, 20188 yr It won't as slashes works on \ not /. In order to help what do you need to know which you don't already know? The reason I ask is it helps with an accurate answer. If you're using $_SERVER['REQUEST_URI'] then it's because you don't know where you are and you need this in order to display. What do you need the url for? Edit: To clarify it's whether you are using real folders, e.g. /realfolder/index.php or if you're using htaccess to rewrite urls e.g. RewriteRule ^products?$ /products.php RewriteRule ^products/([^/]*)$ /products.php?id=$1 [QSA,NC] ... in which case you can $_GET['id'] and it'll work. Edited March 20, 20188 yr by BrowserBugs clarity
March 20, 20188 yr Author Sorry, it's been a while since I've built any website and I have forgotten a few things. I remember the slashes was for \ now.
March 21, 20188 yr Author Followup. I am now reading the folder and using an include to add information into my page. How do I get the computer to send the error message I want, such as "Page Not Found" instead of the error generated by the server when it tries to include something that does not exist?
March 21, 20188 yr I would never recommend using $_SERVER['REQUEST_URI'] for any kind of include, I can't honestly think of when you would need to. Using anything which might trust a user is a bad plan, better to rely on what you use internally. What are you trying to include and why would it not be there?
March 21, 20188 yr Why are you reading a folder? Use $_SERVER['REQUEST_URI'] to identify the folder then use require_once or include to access files within a folder
March 21, 20188 yr Author I'll give an example of one I'm using. I have $page set to "horseback-riding". I have a file in folder /includes/pages called horseback-riding.php. so I include "includes/pages/horseback-riding.php".
March 21, 20188 yr Author The problem is, if someone bookmarks mysite.com/horseback-riding and I no longer have that or if someone types in a url incorrectly, they get an error I had rather eliminate.
March 21, 20188 yr Ah so you want mysite.com/horseback-riding to load /includes/horseback-riding.php? If so wouldn't htaccess be easier? Options +FollowSymlinks RewriteEngine On RewriteBase / RewriteRule ^horseback-riding/?$ /includes/horseback-riding.php [NC] ... NC makes it case insensitive. Then it you ever move the page you could 301 the url to its new home.
March 21, 20188 yr Author Yes, I have that taken care of but let's say someone put in the browser, mysite.com/horse-back-riding they would get Warning: include(): Failed opening 'includes/pages/horse-back-riding.php' for inclusion (include_path='.:/usr/local/php5_4/lib/php') in /home/content/32/5958632/html/mysite.com/index.php on line 106 I'd rather have a "Page Not Found" show up with the rest of my headers/footers/navs etc.
March 21, 20188 yr Dangerous mate, you're letting anyone use mysite.com/showussomething and you're just bolting "showussomething" to .php and then digging around in a folder looking for it?? Edit: Sorry for the grumble. If set on request uri then form an array of legitimate urls and base files and use for comparison, something like; $site_map = array( array('name' => "Horse Riding", 'base' => "horse-riding.php", 'url' => "/horse-riding"), array('name' => "Base Jumping", 'base' => "base-jumping.php", 'url' => "/base-jumping") ); $page_ok = false; $current_location = $_SERVER['REQUEST_URI']; foreach($site_map as $sm) { if($sm['url']==$current_location) { $page_ok = true; $page_base = $sm['base']; } } if(!$page_ok) { // Page is not ok so 404 or whatever. } else { // Page url is ok, so including "/includes/" . $page_base; would work. } Edited March 21, 20188 yr by BrowserBugs
March 22, 20188 yr Author 2 hours ago, NOCK said: Please tell me you haven’t just done that and ignored @BrowserBugs advice? There’s a reason I added the ‘valid_paths’ array. Yes, I have taken all advice. I really appreciate it!
Create an account or sign in to comment