John Sims
John Sims

Reputation: 227

Get username in url

I'd like to get just the username in the url, for example www.blank.com/username

The curl script I'm using returns the full url. I just want the user name here's the script.

<?php
 function curPageURL() {
 $pageURL = 'http';
 if ($_SERVER["HTTPS"] == "on") {$pageURL .= "s";}
 $pageURL .= "://";
 if ($_SERVER["SERVER_PORT"] != "80") {
  $pageURL .=       $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
 } else {
  $pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
  }
 return $pageURL;
}
?>
 <?php
  echo curPageURL();
?>

Upvotes: 0

Views: 1512

Answers (2)

Jan_V
Jan_V

Reputation: 4406

You should find the first /-character of the url (after http://) and get the substr from there. On the page where you got the script from, there's an example on how to do it: http://www.webcheatsheet.com/PHP/get_current_page_url.php

Would be something like this:

return substr($_SERVER["SCRIPT_NAME"],strrpos($_SERVER["SCRIPT_NAME"],"/")+1);

Upvotes: 0

Sarfraz
Sarfraz

Reputation: 382881

You can use the basename function:

$pageName = basename($_SERVER["REQUEST_URI"]);

Upvotes: 1

Related Questions