URL Page Name Without Php ID - php

I have any Url with php extension eg:
http://localhost/test/admin/users.php
http://localhost/test/admin/list.php
http://localhost/test/admin/dates.php
Now For print menu ( navigation ) I Have This :
$pagename = basename($_SERVER['PHP_SELF'], '.php') . '';
if ($pagename = "users") {echo "true";} else {}
My Methods Not Work! How To Get PHP page name ?
Thanks

You have to use == (or ===), not = in your if statement.
You are currently assigning the string "users" to the variable $pagename, so that always evaluates to true.

incorrect IF:
if ($pagename == "users") {echo "true";} else {}

Hi you can use $_SERVER['REQUEST_URI'] and get the file name like this
$self = pathinfo($_SERVER['REQUEST_URI'],PATHINFO_BASENAME);
$self=explode('.',$self);
if( $self[0]=="users"){
do...
}
else{}

You can use $_SERVER['REQUEST_URI'] and parse that value:
$self = pathinfo($_SERVER['REQUEST_URI'], PATHINFO_BASENAME);
if($self == "users")
//do something
else
//do something else

function current_page_name()
{
$url = explode('/',$_SERVER['PHP_SELF']);
$current_url = $url[count($url)-1];
return $current_url;
}
current_page_name();
Hope this will help you?

function currentPageNameByURL()
{
$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;
}

Related

Port Number Being Repeated Twice?

I have a small bug in the construction of my URL, I've setup a test for when I am not using port:80 and for some reason if I use say port:8080 it is applying the port number twice for some reason in the code cant explain it.
public function get_full_url()
{
/** get $_SERVER **/
$server = self::get('SERVER');
$page_url = 'http';
if(isset($server['HTTPS']) and $server['HTTPS'] == 'on')
{
$page_url .= 's';
}
$site_domain = (isset($server['HTTP_HOST']) and trim($server['HTTP_HOST']) != '') ? $server['HTTP_HOST'] : $server['SERVER_NAME'];
$page_url .= '://';
if($server['SERVER_PORT'] != '80')
{
$page_url .= $site_domain.':'.$server['SERVER_PORT'].$server['REQUEST_URI'];
}
else
{
$page_url .= $site_domain.$server['REQUEST_URI'];
}
return $page_url;
}
$_SERVER['HTTP_HOST'] would contain port number as it is set in the Host: header

Show last 5 recently view pages

I am trying the last 5 pages the user viewed on my site to them in a sidebar. Here is the code I am working with:
function curPageURL() {
$pageURL = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' || $_SERVER['SERVER_PORT'] == 443) ? 'https://' : 'http://';
$pageURL .= ($_SERVER['SERVER_PORT'] != "80") ? $_SERVER['SERVER_NAME'].':'.$_SERVER['SERVER_PORT'].$_SERVER['REQUEST_URI'] : $_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'];
return $pageURL;
}
$currentPage = curPageURL();
// $_SESSION['pages'] = $currentPage;
$_SESSION['pages'][] = $currentPage;
if (count($_SESSION['pages']) > 10) {
array_shift($_SESSION['pages']);
if (isset($_SESSION['pagehistory']) && count($_SESSION['pagehistory']) > 10) {
array_shift($_SESSION['pagehistory']);
echo '<h2>Page History</h2>
<ul>';
foreach ($_SESSION['pagehistory'] as $page) {
echo '<li>'.$page.'<li>';
}
echo '</ul>';
}
}
$_SESSION['pagehistory'][] = (!empty($_SERVER['HTTP_REFERER'])) ? $_SERVER['HTTP_REFERER'] : '';
// var_dump($_SESSION); // enable this to show the $_SESSION-arrays made above
When I use this code though nothing appears on my page. So basically I would like to show the user the last 5 pages they have viewed on my site and also like to show the page name not the urls.
You could have a cookie that holds the names of recent pages.
Cookie array for Recently Viewed - need to extract data from array and cap cookie to 5 IDs

PHP unset get parameter?

function getUrlCurrently() {
$pageURL = (#$_SERVER["HTTPS"] == "on") ? "https://" : "http://";
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;
}
I'm using this function to determine the current URL of the page. I want to know if it is possible to extend this function to unset a pre-determined $_GET parameter.
All of my $_GET values are stored in an array. So I can access the specific values by using
$my_array[0]
Is it expensive and not realistic to use my suggested logic to accomplish this task?
EDIT: I only want to print the URL to use it as a link.
My url has GET parameters in it.
Not sure what you really want to do with this, but $_GET (and other super-globals) are not read-only :
You can add values into them,
You can overide values,
And, of course, you can unset() values.
Note, though, that modifying $_GET is often not considered as good-practice : when one reads some code, he expects what's in $_GET to come from the parameters in the URL -- and not from your code.
For instance, you can absolutely do something like this :
unset($_GET['my_item']);
Update to your function:
function getUrlCurrently($filter = array()) {
$pageURL = isset($_SERVER["HTTPS"]) && $_SERVER["HTTPS"] == "on" ? "https://" : "http://";
$pageURL .= $_SERVER["SERVER_NAME"];
if ($_SERVER["SERVER_PORT"] != "80") {
$pageURL .= ":".$_SERVER["SERVER_PORT"];
}
$pageURL .= $_SERVER["REQUEST_URI"];
if (strlen($_SERVER["QUERY_STRING"]) > 0) {
$pageURL = rtrim(substr($pageURL, 0, -strlen($_SERVER["QUERY_STRING"])), '?');
}
$query = $_GET;
foreach ($filter as $key) {
unset($query[$key]);
}
if (sizeof($query) > 0) {
$pageURL .= '?' . http_build_query($query);
}
return $pageURL;
}
// gives the url as it is
echo getUrlCurrently();
// will remove 'foo' and 'bar' from the query if existent
echo getUrlCurrently(array('foo', 'bar'));
To assemble a link with GET parameters in an array try:
unset($my_array['key']);
$url = getUrlCurrently() . '?' . http_build_query($my_array);
See http://www.php.net/manual/en/function.http-build-query.php
This has nthg to do with $_GET. You can just use the existing global data $_SERVER, or getenv, like this :
function GetCurrentUrl($debug=FALSE) {
$pageURL = (strtolower($_SERVER["HTTPS"]) == "on") ? "https://" : "http://";
if ($_SERVER["SERVER_PORT"] != "80") {
$pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
}
else {
$pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
}
// DEBUG
if ($debug) {
$msg = "DEBUG MODE: current URL= ".$pageURL ;
if (function_exists('debug_msg')) {
debug_msg($msg , $debug) ;
}else {
echo $msg ;
}
}
return $pageURL;
}
EDIT: but I see where you are coming from with your $_GET statement. You mean the URI contents some parameters. You'll get them by $_SERVER['REQUEST_URI'], or as better suggested, using http_build_query
EDIT2:
On top of that, with regards to one point of your question, you can also add a work around to setup a "rewriting"-like function as described in this php manual interesting example.
Wouldn't it be easier to user $_SERVER['SCRIPT_URI']?
It returns the full url without query parameters.
//your query string is ?a=1&b=2&c=3
function unset_get($param){
//sets string to (array) $query_string
parse_str($_SERVER['QUERY_STRING'],$query_string);
//removes array element defined by param
unset($query_string[$param]);
//returns modified array as a string
return http_build_query($query_string);
}
print unset_get( 'b');
//returns "a=1&c=3"

Getting the full URL of the current page (PHP) [duplicate]

This question already has answers here:
PHP - Getting Current URL
(3 answers)
Closed 7 years ago.
I'm working on this page: http://localhost/projectname/custom.php
Both <?php echo $_SERVER['REQUEST_URI']; ?> and <?php echo $PHP_SELF; ?> don't give full location. What should I use to grab the full url location?
function selfURL()
{
$s = empty($_SERVER["HTTPS"]) ? '' : ($_SERVER["HTTPS"] == "on") ? "s" : "";
$protocol = strleft(strtolower($_SERVER["SERVER_PROTOCOL"]), "/").$s;
$port = ($_SERVER["SERVER_PORT"] == "80") ? "" : (":".$_SERVER["SERVER_PORT"]);
return $protocol."://".$_SERVER['SERVER_NAME'].$port.$_SERVER['REQUEST_URI'];
}
function strleft($s1, $s2) { return substr($s1, 0, strpos($s1, $s2)); }
There isn't a native method as far as I know, but you could use this:
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;
}
If you are trying to add variables back onto the end of an URL that you are passing through a link tracking script, for example, you could try this:
$URI = array();
foreach($_GET as $key=>$val)
{
if ($key!="link"&&$key!="id"&&$key!="type") $URI[] = "$key=".urlencode($val);
}
if (sizeof($URI)>0) $link.="&".join("&",$URI);
In this case, "link", "id" and "type" were the variables I needed for the tracking, but the URL I wanted to track had a variable on the end of it that got stripped off by my script as if it was part of the query being sent to it; I needed the add it back to the link URL before passing it to header("Location:".$link).
If this is what you are trying to achieve this works great and is shorter than above example.
check this one... a bit long and dirty but works good...
function absolutizeUrl ( $u, $p )
{
$url = parse_url( $u );
$page = parse_url( $p );
if ( strpos( $u , '/' ) === 0 )
{
//already absolute
} else {
$basePath = '';
if (
isset( $page[ 'path' ] )
&& strpos( ltrim( $page[ 'path' ], '/' ), '/' )
)
{
$baseTokens = explode( '/', $page[ 'path' ] );
array_pop( $baseTokens ); // strip basename
$baseTokens[] = $u;
$u = join( '/', $baseTokens );
}
}
if ( ! isset( $url[ 'host' ]))
{
$u = 'http://'.$page[ 'host' ].'/'.ltrim( $u, '/' );
}
return $u;
}
I found this code very helpful
$protocol = strpos(strtolower($_SERVER['SERVER_PROTOCOL']),'https') ===
FALSE ? 'http' : 'https'; // Get protocol HTTP/HTTPS
$host = $_SERVER['HTTP_HOST']; // Get www.domain.com
$script = $_SERVER['SCRIPT_NAME']; // Get folder/file.php
$params = $_SERVER['QUERY_STRING'];// Get Parameters occupation=odesk&name=ashik
$currentUrl = $protocol . '://' . $host . $script . '?' . $params; // Adding all
echo $currentUrl;

Unique entries in an array

I have the following that stores the previous 10 URL's into a session:
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;
}
//Insert Current URL in SESSION
$CurrentPage = curPageURL();
if(strpos($CurrentPage, '/products/'))
{
echo "<div class=\"title\">Recently viewed products</div>
<div id=\"recent\">";
$_SESSION['pages'][] = $CurrentPage;
if ( Count ( $_SESSION['pages'] ) > 10 )
Array_Shift ( $_SESSION['pages'] );
How do I make sure only unique entries are stored?
Thanks,
B
if(!in_array($CurrentPage, $_SESSION['pages']) {
$_SESSION['pages'][] = $CurrentPage;
}
instead of $_SESSION['pages'][] = $CurrentPage try $_SESSION['pages'][$CurrentPage] = 1
/edit: to keep items sorted, unset first:
unset($_SESSION['pages'][$CurrentPage]);
$_SESSION['pages'][$CurrentPage] = 1;
Just after
$_SESSION['pages'][] = $CurrentPage;
you need to add
$_SESSION['pages'] = array_unique($_SESSION['pages']);
Docs are available here
This method requires less processing, as it's a native function. Performing an 'if' on each item in the array could potentially be quite costly.
Could array_unique be of help?

Categories