How do I pull information from URL in PHP? - php

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

Related

Get current page url and replace root with a domain

I would like to change the following code in the following way.
The php script gets the current url of the page of mine.
Now since I am using the descriptions of what i have on my another site i would like to put canonical tag on the page.
So I would like the script to get the url of my site and replace the www.mysite.com to www.domain2.com
How do i make it that the ,," will result the url i want? ( so i can make " rel="canonical" />
It can be beneficial for others aswell, who own more sites and have the same description and dont want to get penalized by Google Panda or have product descriptions from manufacturers.
<?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;
}
?>
I have finally found a solution to that
<?php
$Get_url = "http://" . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
echo str_replace("www.mydomain.com", "www.domain2.com", $Get_url);
?>

How to get specific part of url?

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

PHP get string from a variable and avoid the reference

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;

How to add a custom value to current URL

i use a CMS (elgg : http://www.elgg.org ) working with view, equivalent to theme/template. To change the theme i have to put inside URL the term ?view=mytheme, like :
http://www.mydomain.com/index.php?view=mytheme
If i don't add ?view=mytheme, elgg choose the view by default
So, i need help, for specific users, i want to redirect us to a custom view, and can't see the default view.
I make this in my header :
<?php
if ((string) $_SESSION['user']->type === '2') {
// ???
} else {
echo 'do nothing';
}
I don't know how to take the current url, and simple add ?view=mytheme at the end ?
When a user have the type '2', and see the the url http://www.mydomain.com/index.php
it have to be forward to > http://www.mydomain.com/index.php?view=mytheme
Thanks.
This is kinda dirty, but you could:
// Stole this from here: http://webcheatsheet.com/php/get_current_page_url.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;
}
$useCustomView = ((string)$_SESSION['user']->type) === '2';
$usingCustomView = isset($_GET["view"]);
// Redirect to custom view if user type is 2 and view is not already set
if ($useCustomView && !$usingCustomView) {
$newurl = curPageURL() . '?view=mytheme';
header("Location: $newurl");
}
This is the most basic example I can think of to accomplish what you're asking. This won't quite do the job if you want the URL to support longer query strings so it needs some work.

How to strip a GET property from the URL using PHP

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.

Categories