PHP get string from a variable and avoid the reference - 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;

Related

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

The fastest way to get redirect URL using PHP

How can I get true URL ASAP ? I need to check big number of URLs in a single script (max execution time - 30 sec).
CURL solution takes too much time. I found something like this:
function get_URL($url)
{
$headers = #get_headers($url);
$pattern = '/Location\s*:\s*(https?:[^;\s\n\r]+)/i';
if ($locations = preg_grep($pattern, $headers))
{
preg_match($pattern, end($locations), $redirect);
return $redirect[1];
}
return $url;
}
which seems to works faster. Are there any other, faster ways?
You could use the CURL "multi" feature, which allows you to make multiple requests simultaneously without having to manage multiple processes yourself.
The PHP documentation for curl_multi_init() has some basic examples.
Another tutorial is available here.
Also, if PHP's max execution time is the only thing stopping you and you don't actually care if it takes longer than 30 seconds you can always call set_time_limit() on each iteration of your loop.
$_SERVER['HTTP_REFERER'] contains the previous URL which redirected to the current URL.
Try something like this, from here:
<?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;
}
?>

checking user's current page and condition statement

guys im looking for a comprehensive code to check if a visitor is in index.php then showing some lines to him
i mean i want to consider every possibility for this code
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;
}
$curentUrl = curPageURL();
if (preg_match("/index.php/",$curentUrl) || preg_match("/index.html/",$curentUrl)) {
}
but im looking for a more complete code to see if user is in index page of my website
Actually this curPageURL() function has nothing to do here
if ($_SERVER['PHP_SELF'] == "/index.php")
is enough.
But on most PHP sites index.php doesn't mean only index page. but it can be whole site too.
Without complete circumstances this question is too broad to answer.
What about
$_SERVER['SCRIPT_NAME']

How do I pull information from URL in 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

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