Hello everyone!
I am wondering how I can access the ending of my url address.
For exampel: www.castingvault.eu/willy
And I wand the name "willy" only and then process and use it in my php code.

Usually you write the url like this:
www.castingvault.eu/members.php?mem=willy
and the following is how to access it.

$mem = $_GET['mem'];

Then you access the username: willy.

Can anyone help please
Thank you!

//Willy Lukwago

Dani AI

Generated

A robust, portable way to get the final segment (the "willy" in www.castingvault.eu/willy) is to parse the request URI, strip trailing slashes, decode percent-encodings, then extract the last path segment. was right to point toward the $_SERVER superglobal; 's sample demonstrates the split-and-take-last idea but included a small typo ($SERVER instead of $_SERVER). The approach below avoids accidentally including query strings and works reliably across common hosting setups.

$path = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);
$path = rtrim($path, '/');
$segment = basename($path);        // e.g. "willy"
$username = rawurldecode($segment);

// handle empty or invalid names
if ($username === '') { /* no segment found */ }
if (!preg_match('/^[A-Za-z0-9._-]{1,50}$/', $username)) { /* invalid format */ }

Notes and gotchas: REQUEST_URI contains the path plus query string, so using parse_url(..., PHP_URL_PATH) removes ?params. SCRIPT_NAME or PHP_SELF can behave differently (they show the executing script path and may not include extra path info), and PATH_INFO exists only on some servers — so REQUEST_URI + parse_url is the most consistent choice. Use rawurldecode (rather than urldecode) to correctly handle percent-encoded characters in path segments. Also watch for trailing slashes (they turn the last segment into an empty string) and for collisions with real files or directories — using a prefix like /u/willy or a rewrite to a front controller avoids that.

Security: never trust raw path data. Validate against an allowlist (regex) or check existence in the database, and always use prepared statements for any DB lookup. If link generation is under control, using query parameters (members.php?mem=...) remains the simplest and most explicit option; pretty URLs add SEO and aesthetics but need a bit more routing and input handling.

Recommended Answers

All 2 Replies

You can use the $_SERVER variable to access the URL.

Look down the page for SCRIPT_NAME

Then you could use explode to split the path by a /
The last array item would then be what you are looking for.

Putting what pritaeas is suggesting to code, it would look something like this:

$path = $SERVER['SCRIPT_NAME'];
echo "\nThe path is: $path";

$pieces = explode('/', $path);
echo "\nThe pieces are:";
var_dump($pieces);

$last = end($pieces);
echo "\nThe last piece is: $last";
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.