Jump to content

Path to URL


The Little Guy

Recommended Posts

Is there a built in php function that will convert paths to full urls if not, any suggestions on how to do it?

 

examples (current location: http://site.com/current_location):

something/file.php -> http://site.com/current_location/something/file.php

 

or

 

../something/file.php -> http://site.com/something/file.php

Link to comment
https://forums.phpfreaks.com/topic/265954-path-to-url/
Share on other sites

Are you trying to get the url and then redirect to that once a user has logged in?

 

If so I am currently using this on my site and it works great.

 

<?php
session_start();
$url   = (!empty($_SERVER['HTTPS'])) ? "https://".$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'] : "http://".$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'];
$uid   = $_SESSION['uid'];
if (!isset($uid)) {
	header("location:" . get_option('admin_url') . "login.php?redirect=".$url);
	exit();
	die();
}
?>

Link to comment
https://forums.phpfreaks.com/topic/265954-path-to-url/#findComment-1362774
Share on other sites

I wasn't sure if there was already something for this or not, but here is what I came up with:

 

function path_to_url($current_url, $path){
$pathi = pathinfo($current_url);
$dir = $pathi["dirname"];
$split_path = explode("/", $dir);
$url = "";
if(preg_match("/^\.\./", $path)){
	$total = substr_count($path, "../");
	for($i=0;$i<$total;$i++){
		array_pop($split_path);
	}
	$url = implode("/", $split_path)."/".str_replace("../", "", $path);
}elseif(preg_match("/^\//", $path)){
	$url = $dir.$path;
}elseif(preg_match("/^[a-zA-Z0-9]/", $path)){
	if(preg_match("/^http/", $path)){
		$url = $path;
	}else{
		$url = $dir."/".$path;
	}
}
return $url;
}

Link to comment
https://forums.phpfreaks.com/topic/265954-path-to-url/#findComment-1362793
Share on other sites

Not really sure what that function is doing tbh. It looks like a big mess in all honesty. You look like your trying to convert any path, relative or absolute, to a URL? That can be somewhat tricky as there are some prerequisites such as knowing the root directory to ensure you don't try to append it to the URL - $_SERVER['DOCUMENT_ROOT'] gives you the root directory. Else you hard-code it for a specific file structure.

 

Moreover, I can't actually see where your getting the parent directory for "../" so your function wouldn't ever really work. I also ran a test with what I thought the expected argument formats were and it completely failed. Did you test it?

Link to comment
https://forums.phpfreaks.com/topic/265954-path-to-url/#findComment-1362811
Share on other sites

I did test it, and the results looked perfect.

 

BTW, this function is a little different that the previous post

 

function path_to_url($current_url, $path){
$pathi = pathinfo($current_url);
$dir = $pathi["dirname"];
$base = parse_url(pathinfo($dir, PATHINFO_DIRNAME));
$split_path = explode("/", $dir);
$url = "";
if(preg_match("/^\.\./", $path)){
	$total = substr_count($path, "../");
	for($i=0;$i<$total;$i++){
		array_pop($split_path);
	}
	$url = implode("/", $split_path)."/".str_replace("../", "", $path);
}elseif(preg_match("/^\/|^.\//", $path)){
	$url = $base["scheme"]."://".$base["host"].$path;
}elseif(preg_match("/^[a-zA-Z0-9]/", $path)){
	if(preg_match("/^http/", $path)){
		$url = $path;
	}else{
		$url = $dir."/".$path;
	}
}
return $url;
}

$current = "http://mysite.com/awesome/sub/file.php";
echo "<p><b>Current Location:</b> $current</p>";

echo path_to_url($current, "../../sweet/file.php");
echo "<br>";
echo path_to_url($current, "../sweet/file.php");
echo "<br>";
echo path_to_url($current, "./sweet/file.php");
echo "<br>";
echo path_to_url($current, "/sweet/file.php");
echo "<br>";
echo path_to_url($current, "sweet/file.php");
echo "<br>";
echo path_to_url($current, "http://google.com");

Link to comment
https://forums.phpfreaks.com/topic/265954-path-to-url/#findComment-1362838
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.