PHP get, modify and display incoming URL - php

We are directing all incoming traffic in a directory to a specific page because not all content is ready. Traffic is being redirected to a specific page. It is what it is, and the URL structure aint pretty. I need to get the incoming URL - before the redirect, modify one part of it and display it on the page.
I can get the incoming URL like this
$incoming_link = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
which would ouput this: http://example.com/dir1/dir2/dir3/dir4-lang/Page.php
I need to take that url, and ONLY CHANGE the /dir4-lang/ to /dir4-newLang/ so the URL I display on the page would be http://example.com/dir1/dir2/dir3/dir4-newLang/Page.php
I think I'm on the right track but need some help with:
$url = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
$my_var = 'en-en';
$temp = explode( '/' , $url );
$temp[6] = $my_var;
$url = implode( '/' , $temp );
It is close but the incoming URL is http://example.com/dir1/dir2/dir3/dir4-lang/Foo.php and I am outputting

Try somthing like this:
$my_var = 'dir4-newLang';
$temp = explode( '/' , $_SERVER['REQUEST_URI'] );
$key = array_search('dir4-lang', $temp);
$temp[$key] = $my_var;
$last = count($temp) - 1;
$temp[$last] = strstr($temp[$last], '?');
echo $url = "http://$_SERVER[HTTP_HOST]" . implode( '/' , $temp );
So http://example.com/dir1/dir2/dir3/dir4-lang/Page.php?stuff gonna output http://example.com/dir1/dir2/dir3/dir4-newLang/?stuff

Related

How to get the homepage url?

I am making a redirect page on Wordpress. The PHP will return back to the website homepage. But I can't get back the home page url.
My php file path xampp\htdocs\wordpress\return.php.
And here is my code:
$url = "$_SERVER[HTTP_HOST]";
header('Refresh: 3;url=' . $url);
echo get_option('home');
echo $url;
The $url is localhost8080/wordpess/return.php.
I want to go url : local:8080/wordpress from url : localhost8080/wordpess/return.php.
How can I get back the url local:8080/wordpress?
Thx
Wordpress has a built-in function for that: wp_redirect(see doc)
require_once( dirname(__FILE__) . '/wp-load.php' ); // first you need to load WordPress libraries if you are in an external file.
wp_redirect( home_url() );
exit; // don't forget to exit after a redirection
From what I understand, you're trying to redirect your page from localhost:8080/wordpess/return.php to localhost:8080/wordpess/ using -
$url = "$_SERVER[HTTP_HOST]";
header('Refresh: 3;url=' . $url);
What you need to do is change your $url variable to the location where you want to redirect, which is -
$url = "http://localhost:8080/wordpess/";
header('Refresh: 3; url =' . $url);
Hope that's what you were looking for.
EDIT -
If you don't want to hard code the URL, you can try the following -
$url = "/wordpess";
header("Refresh: 3; url = http://" . $_SERVER['HTTP_HOST'] . $url);
From my understanding of your question, you want to go back one level from the current page. This is it?
If so, you can accomplish that by doing some string manipulation as follows:
<?php
// Given that your current url is in the '$url' var
$url = 'localhost8080/wordpess/return.php';
// Find the position of the last forward slash
$pos = strrpos($url, '/');
// Get a substring of $url starting at position 0 to $pos
// (if you want to include the slash, add 1 to the position)
$new_url = substr($url, 0, $pos + 1);
// Then you can have the redirection code using the $new_url variable
...
Please let me know if I misunderstood.
Hope it helps. Cheers.

Replace the page from a url using php

I have this type of urls stored in a php variable:
$url1 = 'https://localhost/mywebsite/help&action=something';
$url2 = 'https://localhost/mywebsite/jobs&action=one#profil';
$url3 = 'https://localhost/mywebsite/info&action=two&action2=something2';
$url4 = 'https://localhost/mywebsite/contact&action=one&action2=two#profil';
I want to replace the page help, jobs, info, contact with home in a very simple way, something like this:
echo replaceUrl($url1);
https://localhost/mywebsite/home&action=something
echo replaceUrl($url2);
https://localhost/mywebsite/home&action=one#profil
echo replaceUrl($url3);
https://localhost/mywebsite/home&action=two&action2=something2
echo replaceUrl($url4);
https://localhost/mywebsite/home&action=one&action2=two#profil
So here is the solution i found:
function replaceUrl($page){
$pieces = explode("/", $page);
$base = '';
for ($i=0; $i<count($pieces)-1; $i++) $base .= $pieces[$i].'/';
$hash = strpbrk($pieces[count($pieces)-1], '&#');
return $base.'home'.$hash;
}
You'll want to add something like
RedirectMatch 301 help(.*) home$1
to your .htaccess file. I'm not sure PHP is the correct tool for the job.
If you actually want to modify a string with the value of that (which is what your tags and.. comments indicate), you'll want to do:
$url = "https://localhost/mywebsite/help&action=something#profil"
$url = str_replace("help", "home", $url);
echo $url; // https://localhost/mywebsite/home&action=something#profil

How to remove part of the url ratther than the shop url from the currently fetched web page url

In my prestashop shop i have fetched the current web page url by using the below php code.
<?php
$url="http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
echo $url;
?>
My current echo url is http://shoppingworld.com/int/Mens-Tshirts/Fashion.html
My shop url is http://shoppingworld.com/int/
I need to remove the url portion which is coming next to the above shop url.
Try this
<?php
$url_path="http://www.shoppingworld.com/int/Mens-Tshirts/Fashion.html";
$a = parse_url($url_path, PHP_URL_SCHEME);
$b = parse_url($url_path, PHP_URL_HOST);
$url_name_parse=explode('/',$url_path);
$url_name=$url_name_parse[3];
echo ($a . "://" . $b .'/' .$url_name.'/'); ?>
Program Output
http://www.shoppingworld.com/int/
DEMO
You can't directly get that partial url.
Try this,
$url = 'http://shoppingworld.com/int/Mens-Tshirts/Fashion.html';
$parsed = parse_url($url);
$path_array = explode('/', $parsed['path']);
echo $parsed['scheme'] . '//' . $parsed['host'] .'/'. $path_array[1] . '/';
Demo
$arr_url = parse_url($url);
$host = $arr_url['host'];
$service_uri = $arr_url['path'];
read more in php manual about parse_url();

URL Replacement in PHP

I'm trying to change a value in a string that's holding my current URL. I'm trying to get something like
http://myurl.com/test/begin.php?req=&srclang=english&destlang=english&service=MyMemory
to look like
http://myurl.com/test/end.php?req=&srclang=english&destlang=english&service=MyMemory
replacing begin.php for end.php.
I need the end.php to be stored in a variable so it can change, but begin.php can be a static string.
I tried this, but it didn't work:
$endURL = 'end.php';
$beginURL = 'begin.php';
$newURL = str_ireplace($beginURL,$endURL,$url);
EDIT:
Also, if I wanted to replace
http://myurl.com/begin.php?req=&srclang=english&destlang=english&service=MyMemory
with
http://newsite.com/end.php?req=&srclang=english&destlang=english&service=MyMemory
then how would I go about doing that?
Assuming that you want to replace the script filename of the url, you can use something like this :
<?php
$endURL = 'end.php';
$url ="http://myurl.com/test/begin.php?req=&srclang=english&destlang=english&service=MyMemory";
$pattern = '/(.+)\/([^?\/]+)\?(.+)/';
$replacement = '${1}/'.$endURL.'?${3}';
$newURL = preg_replace($pattern , $replacement, $url);
echo "url : $url <br>";
echo "newURL : $newURL <br>";
?>
How do you want them to get to end.php from beigin.php? Seems like you can just to a FORM submit to end.php and pass in the variables via POST or GET variables.
The only way to change what page (end.php, begin.php) a user is on is to link them to another page from that page, this requires a page refresh.
I recently made a PHP-file for this, it ended up looking like this:
$vars = $_SERVER["QUERY_STRING"];
$filename = $_SERVER["PHP_SELF"];
$filename = substr($filename, 4);
// for me substr removed 'abc/' in the beginning of the string, you can of course adjust this variable, this is the "end.php"-variable for you.
if (strlen($vars) > 0) $vars = '?' . $vars;
$resultURL = "http://somewhere.com" . $filename . $vars;

$_GET doesn't work when one of the parameters is a URL

$_GET does not work when one of the parameters to the page is a url.
An external page (which I do not have control on) shows an iframe to my page and it passes parameters of which one is:
turkSubmitTo=http%3A%2F%2Fwww.mturk.com
When on my page I want to extract other parameters, it gives me NULL for everything, but when I remove the "http" it works. Why is that and what can I do to get the other parameters?
EDIT:
You can try it yourself here:
http://www.translate.outofscopes.com/?turkSubmitTo=http%3A%2F%2Fwww.mturk.com
The Array() down there is a print_r of $_GET, you can try to remove the 'http' in the parameter and it will work.
On the localhost it works perfectly.
Try Something like:
$parameters = array();
if (isset($_SERVER['QUERY_STRING'])) {
$pairs = explode('&', $_SERVER['QUERY_STRING']);
foreach($pairs as $pair) {
$part = explode('=', $pair);
$parameters[$part[0]] = urldecode($part[1]);
}
}
You can do this, pretty easily, but you need to control what's creating the url. The trick is to urlencode twice
<?
if ( array_key_exists( 'url', $_GET ) )
{
echo $_GET['url'] . '<br>';
echo urldecode( $_GET['url'] ) . '<br>';
}
$url = 'http://example.com/a/index.php?a=123';
$encUrl = urlencode( urlencode( $url ) );
?>
Not the best - I've seen this fail.
<br>
Much Better

Categories