0

"LoggedIn" does = true and "CurrentUser" isn't "ManselD", yet it's still not writing or creating the file :( And yes my /accounts/logs folder is CHMOD 702

if(!$user == "ManselD"){
    ini_set('date.timezone', 'Europe/London');
    $ip = $_SERVER['REMOTE_ADDR'];
    $txt = "$user Logged In With The Ip: $ip At ".date("h:i")."\n";
    $url = "/accounts/logs/".trim(date('F')."-".date('d')."-".date('Y').".txt");
    $file = fopen($url, "a");
    fwrite($file, $txt);
    fclose($file);
    echo $file;
    if(is_writable("/accounts/logs")){
        echo "It is writable";
    } else{
        echo "It isn't writable";
    }
}

I'm literally stumped and baffled at why this doesn't work :S

FIXED VERSION:

if($user != "ManselD"){
ini_set('date.timezone', 'Europe/London');
$ip = $_SERVER['REMOTE_ADDR'];
$txt = "$user Logged In With The Ip: $ip At ".date("h:i")."\n";
$url = getcwd() . '/accounts/logs/'.trim(date('F')."-".date('d')."-".date('Y').".txt");
file_put_contents($url, $txt, FILE_APPEND);
}
11
  • Testing the value of $file after the fopen() would be a start; test the /accounts/logs directory for is_writeable() via code; do you have display_errors enabled?
    – Mark Baker
    Commented Feb 25, 2013 at 21:17
  • I would also look in the error log files to see if there's an error Commented Feb 25, 2013 at 21:18
  • 1
    !$_SESSION['CurrentUser'] == "ManselD" doesn't do what you think it does. That's equivalent to (!$_SESSION['CurrentUser']) == "ManselD", you most likely want $_SESSION['CurrentUser'] != "ManselD".
    – gen_Eric
    Commented Feb 25, 2013 at 21:19
  • Ok, i put this: if(!$user == "ManselD"){ ini_set('date.timezone', 'Europe/London'); $ip = $_SERVER['REMOTE_ADDR']; $txt = "$user Logged In With The Ip: $ip At ".date("h:i")."\n"; $url = "/accounts/logs/".trim(date('F')."-".date('d')."-".date('Y').".txt"); $file = fopen($url, "a"); fwrite($file, $txt); fclose($file); echo $file; if(is_writable("/accounts/logs")){ echo "It is writable"; }else{ echo "It isn't writable"; } } and it outputs nothing, i added "ini_set('display_errors', '1');" Up the top of the script D:
    – user1933361
    Commented Feb 25, 2013 at 21:36
  • 1
    @ManselD: Again if(!$user == "ManselD") is wrong! That's like doing ((!$user) == "ManselD"), which will convert $user to a boolean (and invert it). Use ($user != "ManselD")
    – gen_Eric
    Commented Feb 25, 2013 at 21:44

1 Answer 1

2

I think the problem (that you are asking about, because there are more if you read the comments) is that you write

$url = "/accounts/logs/".trim(date('F')."-".date('d')."-".date('Y').".txt");

The file paths are not URLs. And while the above is obviously not a URL, it should probably not have the starting slash (/).
A good approach would be to always use absolute paths, possibly using the __FILE__ constant.

4
  • I second this. I personally use getcwd() to get the current working directory path then I add whatever the path is from there. For example: $url = getcwd() . '/myFolder/' . $whateverFileName
    – Cashew
    Commented Feb 25, 2013 at 21:30
  • Thanks. I generally avoid that function, because I write code for Joomla, and my extensions get installed on environments where you NEVER know what the last extension called before your own, did to the current working directory, and you can never really be sure of what that is. :) Commented Feb 25, 2013 at 21:38
  • Hrm, ok well now it says that it isn't writable... I set the CHMOD correctly. It should be working :/
    – user1933361
    Commented Feb 25, 2013 at 21:44
  • @mavrosxristoforos what? lol I have no idea what you're talking about. Glad I'm not involved in any Joomla, then. :P jk I think I know what you mean
    – Cashew
    Commented Feb 26, 2013 at 1:36