I'm trying to create a QR code based on current URL using goqr.me API: https://api.qrserver.com/v1/create-qr-code/?size=150x150&data= so I need generate this code
<img src="https://api.qrserver.com/v1/create-qr-code/?size=150x150&data=**currentURL**">
Things I've tried:
1)
<?php
function getUrl() {
$url = #( $_SERVER["HTTPS"] != 'on' ) ? 'http://'.$_SERVER["SERVER_NAME"] : 'https://'.$_SERVER["SERVER_NAME"];
$url .= ( $_SERVER["SERVER_PORT"] !== 80 ) ? ":".$_SERVER["SERVER_PORT"] : ""; $url .= $_SERVER["REQUEST_URI"];
echo '<img src="https://api.qrserver.com/v1/create-qr-code/?size=150x150&data='.$url;
} ?>
2)
<img src="https://api.qrserver.com/v1/create-qr-code/?size=150x150&data=<?php function getUrl() {
$url = #( $_SERVER["HTTPS"] != 'on' ) ? 'http://'.$_SERVER["SERVER_NAME"] : 'https://'.$_SERVER["SERVER_NAME"];
$url .= ( $_SERVER["SERVER_PORT"] !== 80 ) ? ":".$_SERVER["SERVER_PORT"] : ""; $url .= $_SERVER["REQUEST_URI"]; echo $url;} ?>">
As you can see, I'm not very good at PHP programming. I really hope you can help me.
In you second example you declare the function and don't call it, so nothing gets written.
The second example can be changed to this:
<img src="https://api.qrserver.com/v1/create-qr-code/?size=150x150&data=<?php function getUrl() {
$url = #( $_SERVER["HTTPS"] != 'on' ) ? 'http://'.$_SERVER["SERVER_NAME"] : 'https://'.$_SERVER["SERVER_NAME"];
$url .= ( $_SERVER["SERVER_PORT"] !== 80 ) ? ":".$_SERVER["SERVER_PORT"] : ""; $url .= $_SERVER["REQUEST_URI"]; echo $url;} getUrl(); ?>">
or it would look better like this:
<?php
function getUrl() {
$url = #( $_SERVER["HTTPS"] != 'on' ) ? 'http://'.$_SERVER["SERVER_NAME"] : 'https://'.$_SERVER["SERVER_NAME"];
$url .= ( $_SERVER["SERVER_PORT"] !== 80 ) ? ":".$_SERVER["SERVER_PORT"] : ""; $url .= $_SERVER["REQUEST_URI"];
return $url;
}
?>
<img src="https://api.qrserver.com/v1/create-qr-code/?size=150x150&data=<?= getUrl(); ?> ">
Related
I have a redirect character strip script, that takes the original URL, strips the requested strings out of it (foo , bar) and then redirects to the same URL only without these strings.
It's currently set up to work with HTTP Only, as users always requests the HTTP page. But now I'm adding HTTPS, so some users will land on HTTPS. in that case, I'd like the redirect to be to the HTTPS.
How Can I do it?
I've tried simply changing:
$url = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
Into:
$url = "://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
But it created an invalid request (mydomain.com//mydomain.com....)
CODE:
function unparse_url($parsed_url) {
$scheme = isset($parsed_url['scheme']) ? $parsed_url['scheme'] . '://' : '';
$host = isset($parsed_url['host']) ? $parsed_url['host'] : '';
$port = isset($parsed_url['port']) ? ':' . $parsed_url['port'] : '';
$user = isset($parsed_url['user']) ? $parsed_url['user'] : '';
$pass = isset($parsed_url['pass']) ? ':' . $parsed_url['pass'] : '';
$pass = ($user || $pass) ? "$pass#" : '';
$path = isset($parsed_url['path']) ? $parsed_url['path'] : '';
$query = !empty($parsed_url['query']) ? '?' . trim($parsed_url['query'], '&') : '';
$fragment = isset($parsed_url['fragment']) ? '#' . $parsed_url['fragment'] : '';
return "$scheme$user$pass$host$port$path$query$fragment";
}
function strip_query($url, $query_to_strip) {
$parsed = parse_url($url);
$parsed['query'] = preg_replace('/(^|&)'.$query_to_strip.'[^&]*/', '', $parsed['query']);
return unparse_url($parsed);
}
$url = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
$url2 = (strip_query($url, 'foo')); # query to strip - foo
$new_url = (strip_query($url2, 'bar')); # strip also - bar
$filtered = array_filter(array_keys($_GET), function($k) {
return strpos($k, 'foo') === 0;
});
if ( !empty($filtered) ) {
$_SESSION['trackParam'] = $_GET; // #### Save original request data and url before redirection
$_SESSION['REQUEST_URI'] = $_SERVER[REQUEST_URI];
$_SESSION['redirected'] = true;
header ("Location: $new_url");
}
You can use $_SERVER['HTTPS'] to decide whether to use https in your $url:
function check_https() {
return (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off')
|| $_SERVER['SERVER_PORT'] == 443;
}
$url = (check_https() ? "https" : "http")
."://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
You can check if the request is made to HTTPS and put a condition:
if( isset($_SERVER['HTTPS'] ) ) { $url= ... } else {$url= ... }
I'm trying to get the full url together with all the query strings of this url
http://localhost/test/searchprocess.php?categ=vessel&keyword=ves&search-btn=Search&page=5
I tried to use this function but it doesn't give me all of the query strings. It only saves the first query string.
function getCurrentURL() {
$currentURL = (#$_SERVER["HTTPS"] == "on") ? "https://" : "http://";
$currentURL .= $_SERVER["SERVER_NAME"];
if($_SERVER["SERVER_PORT"] != "80" && $_SERVER["SERVER_PORT"] != "443") {
$currentURL .= ":".$_SERVER["SERVER_PORT"];
}
$currentURL .= $_SERVER["REQUEST_URI"];
return $currentURL;
}
When I echo getcurrentURL(); it only gives me http://localhost/test/searchprocess.php?categ=vessel
How can I be able to get the full url?
I think your code is almost right. Try this:
$base_url = ( isset($_SERVER['HTTPS']) && $_SERVER['HTTPS']=='on' ? 'https' : 'http' ) . '://' . $_SERVER['HTTP_HOST'];
$url = $base_url . $_SERVER["REQUEST_URI"];
You want the parse_url() function:
http://php.net/manual/en/function.parse-url.php
$url = 'http://username:password#hostname:9090/path?arg=value#anchor';
var_dump(parse_url($url, PHP_URL_QUERY));
which returns
string(9) "arg=value"
function getUrl() {
$url = #( $_SERVER["HTTPS"] != 'on' ) ? 'http://'.$_SERVER["SERVER_NAME"] : 'https://'.$_SERVER["SERVER_NAME"];
$url .= ( $_SERVER["SERVER_PORT"] !== 80 ) ? ":".$_SERVER["SERVER_PORT"] : "";
$url .= $_SERVER["REQUEST_URI"];
return $url;
}
?>
if ($sessionUser == 1 || $sessionSeller == 1 || $sessionadmin == 1) { ?>
:*
" onClick="validate_form('frm_comment')" />
" />
" onClick="window.location.href='login.php?event=Account&url='"'" />
Still shows get url is like this : /login.php?event=Account&url=
how to get page like /login.php?event=Account&url=205 .....or anything
<?php
$getUrl = $_GET['url'];
echo $getUrl;
?>
I use the "Current url" function to get the current link when user changing page language
$uri = explode('&', $_SERVER['REQUEST_URI']);
$uri = $uri[0];
$url = (!empty($_SERVER['HTTPS'])) ? "https://".$_SERVER['SERVER_NAME'].$uri : "http://".$_SERVER['SERVER_NAME'].$uri;
Problem is, when I have a link like this:
http://127.0.0.1/index.php?id=shop&id2=13&lang=lt
id2, of course, disappears. What can I do about this? It is possible if id2 is set to use explode with a second & or something like this?
You can use the parse_url function, here is an example:
$uri = parse_url( $_SERVER['REQUEST_URI']);
$protocol = !empty($_SERVER['HTTPS']) ? 'https://' : 'http://';
$url = $protocol . $_SERVER['SERVER_NAME'] . $_SERVER['SCRIPT_NAME'] . '?' . ( isset( $uri['query']) ? $uri['query'] : '');
I did not see in your code where you get the script's filename, so I used $_SERVER['SCRIPT_NAME'].
Edit: My mistake, I did not see that you need to manipulate / remove the last $_GET parameter. Here is an example on how to do that using a method similar to the above in conjunction with parse_str. Note that this method will work regardless of the location of the lang parameter, it does not have to be the last one in the query string.
$protocol = !empty($_SERVER['HTTPS']) ? 'https://' : 'http://';
$params = array();
if( isset( $_SERVER['QUERY_STRING']) && !empty( $_SERVER['QUERY_STRING']))
{
parse_str( $_SERVER['QUERY_STRING'], $params);
$params['lang'] = 'anything';
// unset( $params['lang']); // This will clear it from the parameters
}
// Now rebuild the new URL
$url = $protocol . $_SERVER['SERVER_NAME'] . $_SERVER['SCRIPT_NAME'] . ( !empty( $params) ? ( '?' . http_build_query( $params)) : '');
Thanks to #Yzmir Ramirez for an improvement in the second version that eliminates the extraneous call to parse_url.
$uri = explode('&', '/index.php?id=shop&id2=13&lang=lt');
$uri = $uri[0];
echo $uri; //echos /index.php?id=shop
You'll want to use
$_SERVER['QUERY_STRING']
to get just the query string. And if you want to keep all the variables, then just keep $uri set to the explode then cycle through them with a foreach.
$uri = explode('&', $_SERVER['QUERY_STRING'];
foreach ($uri as $var_val) {
$var_val = explode('=', $var_val);
$var = $var_val[0];
$val = $var_val[1];
}
Try this: http://codepad.org/cEKYTAp8
$uri = "http://127.0.0.1/index.php?id=shop&id2=13&lang=lt";
$uri = substr($uri, 0, strrpos($uri, '&'));
var_dump($uri); // output: string(41) "http://127.0.0.1/index.php?id=shop&id2=13"
Here is the code I have always used, it also supports ports.
$protocol = (!isset($_SERVER["HTTPS"]) || strtolower($_SERVER["HTTPS"]) == "off") ? "http://" : "https://";
$port = ((isset($_SERVER["SERVER_PORT"]) &&
// http:// protocol defaults to port 80
(($_SERVER["SERVER_PORT"] != "80" && $protocol == "http://") ||
// https:// protocol defaults to port 443
($_SERVER["SERVER_PORT"] != "443" && $protocol == "https://")) &&
// Port is not in http host (port is followed by : at end of address)
strpos($_SERVER["HTTP_HOST"], ":") === false) ? ":" . $_SERVER["SERVER_PORT"] : '');
return $protocol . $_SERVER["HTTP_HOST"] . $port . $_SERVER["REQUEST_URI"];
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;