Skip to content
View in the app

A better way to browse. Learn more.

Web Designer Forum

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Reading the url

Featured Replies

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.

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.

 

  • 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.

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 by BrowserBugs
clarity

  • 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.

  • 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?

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?

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

  • 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".

  • 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.

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.

  • 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.

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?? :unsure:

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 by BrowserBugs

  • 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

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.