Jump to content

Path to URL


The Little Guy

Recommended Posts

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
Share on other sites

I'm assuming you then know the full URL of the page you're dealing with then? Why not simply create your own function?

 

It should be pretty straight forward, just explode by '/', and check if it's a .. or an actual folder name, and change the base url depending.

Link to comment
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
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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.