I have the following function that get's the current page URL:
<?php
// get current page url
function currentPageUrl() {
$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"];
}
echo $pageURL;
}
?>
Which prints:
http://localhost/gallery.php?id=23&type=main
I want to remove "&type=main" which is present in the url. So before echoing $pageURL I add the following line:
$pageUrl = preg_replace("&type=main", "", $pageURL);
But it still returns the full url including type=main. How can I get rid of that from the url?
Another solution could be to :
use parse_url or $_SERVER['QUERY_STRING'] to extract the list of parameters as a string
use parse_str to transform the query string to an array containing each parameter and its value -- indexed by parameters names.
Do some magic on that array :
do what you have to to filter it
For example, unset($array['type']); could probably help ;-)
If needed, add more parameters to that array
And, then, use http_build_query to re-build a query-string.
A bit more complex than string manipulations, of course -- but much more reliable, I'd say ;-)
You can throw a url into parse_url. It will return an array from which you can rebuild as you see fit.
Try this:
$pageUrl = str_replace('&type=main', '', $pageURL);
did you try any other $_SERVER variables?
there are plenty and some of them already contain everything you need without any replace
phpinfo(32);
will show you all
PHP identifiers are case sensitive. You probably meant to assign it to the same variable.
$pageURL = preg_replace("&type=main", "", $pageURL);
Either that, or you need to change the remnant of code to use $pageUrl instead of $pageURL.
Related
I am working on search function which I want make my search more easier and I had store href inside my db. Therefore, I need to get specific part of current page url eg : abc.php.
But now I only can get full url which is eg : http://abc_system/user/abc.php. Is it one of the solution is used substring?I am looking for some help. Hope you guys can help me out. Thanks in advanced.
This is my code which return url result:
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;
}
You need to use basename() function for get your filename from the URL string.
<?php
$url = "http://google.com/sdfsaf/abcd.php";
echo basename($url); // It will returns abcd.php
?>
Demo
Use **parse_url()** if you want to split url to its components,
For more info, Refer : http://www.php.net/manual/en/function.parse-url.php
I'd first like to mention that I'm not looking for an alternative method. I actually want to try and figure out how to fix this. I am sure a lot of you are familiar with this code:
$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"];
}
This code gets the URL of the current page it's used on. What I want to do is get a string from the URL and put it into a session. I've tried using code such:
$_SESSION['pageURL']=$pageURL;
//and
$_SESSION['pageURL']= (string)$pageURL;
So far all of my effort have been fruitless, any help would be much appreciated...
$pageURL already is a string, so:
$_SESSION['pageURL']=$pageURL;
should work without any problems.
Are you using session_start() somewhere?
$_SESSION['pageURL']=$pageURL; //Obviously won't work
That actually works perfectly... Make sure you have called session_start()?
session_start();
$_SESSION['pageURL']=$pageURL;
ok i have a url from $_SERVER['REQUEST_URI']
lets say it give us a url
http://localhost/controller/method
i have tried something like
explode('/',$_SERVER['REQUEST_URI'])
and it gave us like
array
0 => string '' (length=0)
1 => string 'controller' (length=10)
2 => string 'method' (length=6)
what is the best way to get the controller or method ? or removeing the 0 in the array ? ( first array ) ?
so it will be like
$controller = 'controller';
$method = 'method';
from above inputs.
maybe about list ? still no clue using list().
edit heres what ive done so far
$this->url = str_replace(config('foldertoscript'), NULL, $_SERVER['REQUEST_URI']);
$hello = explode('/',$this->url);var_dump($hello);
array_shift($hello);
list($controller,$method) = $hello;
var_dump($hello,$controller);
in a class
Thanks for looking in.
Adam Ramadhan
To remove the first element of an array, you can use array_shift().
$_SERVER['REQUEST_URI'] gives you the url without the "http://www.yoursite.com".
You can use something like this
<?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;
}
echo curPageURL();
?>
Hope this helps.
Use array_shift to remove the first array item.
http://php.net/manual/en/function.array-shift.php
Example:
$your_array = array_shift($your_array);
$controller = $your_array[0];
$method = $your_array[1];
For the same matter I use url_rewriting.
I have a rule that says ^([a-zA-Z0-0-_\/.]+)$ index.php?url=$1
(this is not a copy paste from my code, but you get the idea)
then if you say $_URL = $_REQUEST["url"];
$directive = explode("/",$_URL);
you will get what you need, as for the parameters you could say module/method/id/1/data/2
you have to take care of your parameters and it works if you use the GET method for
navigation only(as it should be used). Also makes the stuff much safer as no one can send SQL
injections via get or any "smart" directives.
Lets say the url is http://example.com/product/3 and I only want to retrieve what is after http://example.com/product/. I found this, it echos the domain but how do I get the three. Its this method reliable? I'm using codeIgniter also.
<?php
function curPageURL() {
$pageURL = 'http';
$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();
?>
Use $this->uri->segment(n). Documentation for it is here:
http://codeigniter.com/user_guide/libraries/uri.html
For your code, you would use:
$curPageURL = $this->url->segment(2)
Edited: Fixed a bug in the code.
Yes, use the URI class of the codeigniter API.
The base_url is segment zero, so in your case, products would be segment 1 and the id segment 2.
$product_id = $this->uri->segment(2)
As chetan mentioned this is clearly documented in the user guide.
You should use parse_url instead:
http://php.net/manual/es/function.parse-url.php
I have a template I made that sets variables that rarely change, call my headers, calls my banner and sidebar, loads a variable which shows the individual pages, then calls the footer. In one of my headers, I want the URL of the page in the user's address bar. Is there a way to do this?
Currently:
<?php
$title = "MySite - Contacts";
include("header.php");
.
.
.
?>
The main variables you'll be intersted in is:
$_SERVER['REQUEST_URI'] Holds the path visited, e.g. /foo/bar
$_SERVER['PHP_SELF'] is the path to the main PHP file (NOT the file you are in as that could be an include but the actual base file)
There are a ton of other useful variables worth remembering in $_SERVER, so either just:
print_r($_SERVER);
or just visit the doc at http://php.net/manual/en/reserved.variables.server.php
the Web address of the Page being called, can be obtained from the following function ::
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;
}
I have been using this in many places, found on google.
It sounds like $_SERVER['REQUEST_URI'] is what you're after.