...

View Full Version : PHP symlinks - not sure what's happening



RTrev
04-01-2007, 03:40 AM
I'm trying to code a page which will create a symbolic link to itself, so that when that link is clicked on it will open the same page as is currently displaying, but with a different URL. The results of the symlink operation are baffling me.



<?php

$referer = $_SERVER["HTTP_REFERER"];
$current = $_SERVER["PHP_SELF"];

echo "The current page we are on is: $current<br>";
echo "The page we came from was: $referer<br>";

$next_doc = substr(md5(rand(0,9999)),3,4).".php";

echo "The doc we are going to next is: $next_doc<br>";

$status = symlink($current, $next_doc);
if (false !== $status) {
echo "Created $next_doc - ready to link to it.<br>";
} else {
echo "Failed to create $next_doc<br>";
}

echo "<a href=\"$next_doc\">Link to new doc</a>";

?>


When this is run, the output looks like this:



The current page we are on is: /test/test.php
The page we came from was:
The doc we are going to next is: e58c.php
Created e58c.php - ready to link to it.
Link to new doc


That last line is a link, as intended, and it points where I intended it to; a randomly named file in the current directory.

The file is created, it's the identical size of the current file, it has the same protections, everything looks like it should work fine, but all I get is a "page not found" error.

I thought a symbolic link was supposed to be a way to access a file via a shortcut or alternate path. In my case, it appears to be making an exact duplicate of the file, even down to the having the same MD5SUM value. The only difference is that the new page won't load, even if typed in directly as a URL.

Any clues what I'm doing wrong, or misunderstanding? I suspect there are multiple misunderstandings going on concurrently here! :confused:

iLLin
04-01-2007, 05:31 AM
I tried this


$referer = $_SERVER["HTTP_REFERER"];
$current = $_SERVER["PHP_SELF"];

echo "The current page we are on is: $current<br>";
echo "The page we came from was: $referer<br>";

$next_doc = substr(md5(rand(0,9999)),3,4).".php";

echo "The doc we are going to next is: $next_doc<br>";

if (!symlink($current, $next_doc)) {
echo "Failed to create $next_doc<br>";
} else {
echo "Created $next_doc - ready to link to it.<br>";
}

echo "<a href=\"$next_doc\">Link to new doc</a>";

It created a "link" to the file, but when I try to access it, I get a Forbidden message since I have my server configured in that manner. But the code worked as expected. It shouldn't create a duplicate file. Are you on a linux or windoz box?

RTrev
04-01-2007, 05:44 AM
Hi iLLin,

I'm on a Linux system, hosted by 1&1. Your mention of the server configuration reminded me of an .htaccess option of +FollowSymLinks but I found it was already turned on in my .htaccess. I tried turning it off, but still the same problem.

So, the code is okay.. I just need to figure out if I can get the server to allow it to work..

Thanks!

Bob

firepages
04-01-2007, 03:26 PM
A symbolic link should have the same MD5 checksum as the original file, so it should look to all external calls (like the internal php stat() calls) as if it were the linked file, try a call to is_link() or linkinfo() to see if the file is really a symbolic link or not.

Then try chmodding the link to say 0775 or 0777 to see if that makes it visible (remember the link is owned by PHP or the webserver)

as a last resort, exec the unix call <?exec('ln -s file.txt file_link.txt');?>



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum