GET URL from pages that access my Image - php

Imagine any webpage ex: example1.com/something... using this html:
<img src="http://www.example2.com/controller/showImage" >
My controller in web2.com is something like:
function showImage() {
getIP(); //return USER IP (working)
getURL(); //return example1.com/something... (not working it returns always 'http://www.example2.com/controller/showImage')
addMetricsSQL($ip, $url); //add ip and url in database (working)
header('Content-Type: image/jpeg'); //headers
readfile($this->$path_img); //a path
}
My current function is:
public function getURL() {
$ssl = ( ! empty( $_SERVER['HTTPS'] ) && $_SERVER['HTTPS'] == 'on' );
$sp = strtolower( $_SERVER['SERVER_PROTOCOL'] );
$protocol = substr( $sp, 0, strpos( $sp, '/' ) ) . ( ( $ssl ) ? 's' : '' );
$port = $_SERVER['SERVER_PORT'];
$port = ( ( ! $ssl && $port=='80' ) || ( $ssl && $port=='443' ) ) ? '' : ':'.$port;
$host = ( false && isset( $_SERVER['HTTP_X_FORWARDED_HOST'] ) ) ? $_SERVER['HTTP_X_FORWARDED_HOST'] : ( isset( $_SERVER['HTTP_HOST'] ) ? $_SERVER['HTTP_HOST'] : null );
$host = isset( $host ) ? $host : $_SERVER['SERVER_NAME'] . $port;
return $protocol . '://' . $host . $_SERVER['REQUEST_URI'];
}
But like I said, it only returns example2.com/controller/showImage
My questions:
Is posible to get the right URL(in this case
example1.com/something...) using PHP?
How the getURL() function should be?
Thanks in advance

It was painfull but the answer to my question is:
public function getURL() {
return getenv('HTTP_REFERER', true) ?: getenv('HTTP_REFERER');
}
HTTP_REFERER Makes all the magic.

Related

Express Checkout Paypal Payment Gateway For Woocommerce have issues with Polylang

If we set the language selection URL from the Polylang plugin URL modification at that time PayPal checkout button not working on the cart and checkout page on the website.
Please once check and let us know if you have any solution to this issue.
Thank you.!
add_filter('woocommerce_api_request_url', 'wt_woocommerce_api_request_url', 10, 3);
function wt_woocommerce_api_request_url($api_request_url, $request, $ssl) {
if ( is_null( $ssl ) ) {
$scheme = parse_url( home_url(), PHP_URL_SCHEME );
} elseif ( $ssl ) {
$scheme = 'https';
} else {
$scheme = 'http';
}
$lang = 'en';
if ( function_exists('pll_current_language') ) {
$lang = pll_current_language();
}
if ( strstr( get_option( 'permalink_structure' ), '/index.php/' ) ) {
$api_request_url = trailingslashit( home_url( $lang .'/index.php/wc-api/' . $request, $scheme ) );
} elseif ( get_option( 'permalink_structure' ) ) {
$api_request_url = trailingslashit( home_url( $lang .'/wc-api/' . $request, $scheme ) );
} else {
$api_request_url = add_query_arg( 'wc-api', $request, trailingslashit( home_url( '', $scheme ) ) );
}
return esc_url_raw( $api_request_url );
}

Cannot find where a redirect is specified

I've recently become the maintainer of a Wordpress site (I'm completely new to wordpress) and I'm having some difficulty determining where a redirect is specified.
I've checked the .htaccess file, and there's nothing specified in there. As far as I can tell, the rewrite rules aren't the cause.
I've tried deleting the page being redirected from and re-creating it, and the redirect still occurs.
My question is: where can you specify a redirect? I've run out of ideas of where to look.
one of my client want to custom url like
https://www.qsleap.com/gmat/resources as you know in wordpress evry request is catch by index.php . request filter catches the request and
call the page .
Read this code it may give you any idea.
function permalinks_customizer_request_before($query ){
$uri=$_SERVER['REQUEST_URI'];
$match= preg_match('/(gmat|gre|sat|lsat|cat)(\/resources\/tags\/)
(.*)\/(articles|videos|concept-notes|qna)/', $uri,$matches);
//$match=
preg_match('/(gmat|gre|sat|lsat|cat)/\resources/\tags/stanford-
gsb/\articles|videos|concept-notes)/?$', $uri,$matches);
if($match){
$url = parse_url( get_bloginfo( 'url' ) );
$url = isset( $url['path']) ? $url['path'] : '';
$request = ltrim( substr( $_SERVER['REQUEST_URI'], strlen( $url ) ), '/' );
$request = ( ( $pos = strpos( $request, '?' ) ) ? substr( $request, 0, $pos ) : $request );
if ( ! $request )
return $query;
$original_url="?page_name=tags&exam=".$matches[1]."&post_tag=".$matches[3]."&post_type=".$matches[4];
if ( $original_url !== null ) {
$original_url = str_replace('//', '/', $original_url);
if ( ( $pos = strpos( $_SERVER['REQUEST_URI'], '?' ) ) !== false ) {
$queryVars = substr( $_SERVER['REQUEST_URI'], $pos + 1 );
$original_url .= ( strpos( $original_url, '?' ) === false ? '?' : '&') . $queryVars;
}
$oldRequestUri = $_SERVER['REQUEST_URI'];
$oldQueryString = $_SERVER['QUERY_STRING'];
$_SERVER['REQUEST_URI'] = '/' . ltrim( $original_url, '/' );
$_SERVER['QUERY_STRING'] = ( ( $pos = strpos( $original_url, '?' ) ) !== false ? substr( $original_url, $pos + 1 ) : '' );
parse_str( $_SERVER['QUERY_STRING'], $queryArray );
$oldValues = array();
global $wp;
$wp->parse_request();
$query = $wp->query_vars;
if ( is_array( $queryArray ) ) {
foreach ( $queryArray as $key => $value ) {
$oldValues[$key] = $_REQUEST[$key];
$_REQUEST[$key] = $_GET[$key] = $value;
$query[$key]=$value;
}
}
$_SERVER['REQUEST_URI'] ='';
$_SERVER['QUERY_STRING']='';
}
}
return $query;
}
add_filter( 'request','permalinks_customizer_request_before',0);
function wp_url_rewrite_templates() {
if (get_query_var( 'page_name' ) && get_query_var( 'page_name'
)=='tags') {
add_filter( 'template_include', function() {
$template= dirname( __FILE__ ) . '/page-tags.php';
return $template;
});
}
}
add_action( 'template_redirect', 'wp_url_rewrite_templates' ,4 );
I think the easiest way for you to remove the redirects will be with this plugin.
https://redirection.me/
After you install it and activate it. From the Wordpress Admin
Tools > Redirection
You'll see a list of redirects, and add/remove any.

php for full url yoast

I am new to php.
I am using Wordpress and yoast.
However, the yoast plugin is not writing the canonical correctly. I wish to write the canonical so that it uses the syntax
<link rel="canonical" href="https://www.travelideology.com/about/">
However, it is writing
<link rel="canonical" href="https:/about/">
Would be happy if someone can let me know what is wrong with my code.
The code is as follows:
function my_wpseo_canonical( $canonical ) {
global $post;
$attachment = get_post( $post->ID );
$protocol = ($_SERVER['HTTPS'] && $_SERVER['HTTPS'] != "off") ? "https" : "http";
$cleansed = $protocol . "://" . $_SERVER['HTTP_HOST'];
$cleansed2 = get_permalink($attachment);
$canonical = $cleansed . $cleansed2;
return $canonical;
}
add_filter( 'wpseo_canonical', 'my_wpseo_canonical' );
try
function my_wpseo_canonical( $canonical ) {
global $post;
$attachment = get_post( $post->ID );
$protocol = ($_SERVER['HTTPS'] && $_SERVER['HTTPS'] != "off") ? "https" : "http";
$cleansed = $protocol . "://" .site_url(). $_SERVER['HTTP_HOST'];
$cleansed2 = get_permalink($attachment);
$canonical = $cleansed . $cleansed2;
return $canonical;
}

Checking a variable for characters, changing it if they exist

I have this code that is ment to stop people entering dots and slashes into query strings.
The string is defined to a variable, then if the variable containes a '.' or a '/' i would like to replace them with forbidden and main.
<?php
if( strpos( $page, '/' ) === true ) { $page = 'forbidden'; $inner_page = 'main'; }
elseif( strpos( $page, '.' ) === true ) { $page = 'forbidden'; $inner_page = 'main'; }
elseif( strpos( $inner_page, '/' ) === true ) { $page = 'forbidden'; $inner_page = 'main'; }
elseif( strpos( $inner_page, '.' ) === true ) { $page = 'forbidden'; $inner_page = 'main'; }
?>
This code (isAllowedURL.php) is being loaded into this code:
<?php
$page = 'home';
$inner_page = 'main';
if( isset( $_GET['page'] ) )
{
$page = htmlspecialchars( $_GET['page'] );
}
if( isset( $_GET['subpage'] ) )
{
$inner_page = htmlspecialchars( $_GET['subpage'] );
}
include ('/indexBuilder/linkSecurity/isAllowedURL.php');
?>
As you can see, it changes the variables to the query if it exists and i would like to change it back if the variable containes the banned characters.
Why does this code not function as wanted?
-I changed the true to false and every page was made forbidden, changed it back and none where.
SOLVED:
You need to use !== to see if they are present. Also you cannot use true with strpos.
if( strpos( $page, '/' ) !== false ) { $page = 'forbidden'; $inner_page = 'main'; }

PHP Get URL with Parameter

I want to get a URL and its parameters. Example:
www.someweb.com/somepage.php?id=10
How to get only somepage.php?id=10?
I'm already tried REQUEST_URI and PHP_SELF but it only gives me www.someweb.com/somepage.php or somepage.php. What I want to is just the page name and its parameter(s).
basename($_SERVER['REQUEST_URI']);
This will return all URLs with page name. (e.g.: index.php?id=1&name=rr&class=10).
for complette URL with protocol, servername and parameters:
$base_url = ( isset($_SERVER['HTTPS']) && $_SERVER['HTTPS']=='on' ? 'https' : 'http' ) . '://' . $_SERVER['HTTP_HOST'];
$url = $base_url . $_SERVER["REQUEST_URI"];
$_SERVER['PHP_SELF'] for the page name and $_GET['id'] for a specific parameter.
try print_r($_GET); to print out all the parameters.
for your request echo $_SERVER['PHP_SELF']."?id=".$_GET['id'];
return all the parameters echo $_SERVER['PHP_SELF']."?".$_SERVER['QUERY_STRING'];
function curPageName() {
return substr($_SERVER["SCRIPT_NAME"],strrpos($_SERVER["SCRIPT_NAME"],"/")+1);
}
echo "The current page is ".curPageName()."?".$_SERVER['QUERY_STRING'];
This will get you page name , it will get the string after the last slash
Here's probably what you are looking for: php-get-url-query-string. You can combine it with other suggested $_SERVER parameters.
Here's a very thorough function I use to get the full URL:
function get_full_url( bool $include_query = true ) {
$protocol = ( ! empty( $_SERVER['HTTPS'] ) && strtolower( $_SERVER['HTTPS'] ) !== 'off' )
|| ( isset( $_SERVER['HTTP_X_FORWARDED_PROTO'] ) && strtolower( $_SERVER['HTTP_X_FORWARDED_PROTO'] ) === 'https' )
|| ( ! empty( $_SERVER['HTTP_FRONT_END_HTTPS'] ) && strtolower( $_SERVER['HTTP_FRONT_END_HTTPS'] ) !== 'off' )
|| ( isset( $_SERVER['SERVER_PORT'] ) && intval( $_SERVER['SERVER_PORT'] ) === 443 ) ? 'https' : 'http';
$uri = $protocol . '://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
return $include_query ? $uri : str_replace( '?' . $_SERVER['QUERY_STRING'], '', $uri );
}

Categories