The Little Guy Posted July 19, 2012 Share Posted July 19, 2012 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 Quote Link to comment https://forums.phpfreaks.com/topic/265954-path-to-url/ Share on other sites More sharing options...
andrew_biggart Posted July 19, 2012 Share Posted July 19, 2012 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(); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/265954-path-to-url/#findComment-1362774 Share on other sites More sharing options...
The Little Guy Posted July 19, 2012 Author Share Posted July 19, 2012 no, I am scraping web pages for urls, and then saving them in a database and some urls look like this: <a href="/somewhere/file.php">Click Me</a> <a href="../../somepage/file.php">Click Me</a> Quote Link to comment https://forums.phpfreaks.com/topic/265954-path-to-url/#findComment-1362778 Share on other sites More sharing options...
xyph Posted July 19, 2012 Share Posted July 19, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/265954-path-to-url/#findComment-1362790 Share on other sites More sharing options...
The Little Guy Posted July 19, 2012 Author Share Posted July 19, 2012 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; } Quote Link to comment https://forums.phpfreaks.com/topic/265954-path-to-url/#findComment-1362793 Share on other sites More sharing options...
cpd Posted July 19, 2012 Share Posted July 19, 2012 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? Quote Link to comment https://forums.phpfreaks.com/topic/265954-path-to-url/#findComment-1362811 Share on other sites More sharing options...
The Little Guy Posted July 19, 2012 Author Share Posted July 19, 2012 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"); Quote Link to comment https://forums.phpfreaks.com/topic/265954-path-to-url/#findComment-1362838 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.