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);
}
tshepang
  • 12,111
  • 21
  • 91
  • 136
  • 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 Feb 25 '13 at 21:17
  • I would also look in the error log files to see if there's an error – Moshe Shaham Feb 25 '13 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 Feb 25 '13 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: –  Feb 25 '13 at 21:36
  • Please edit your original post if you want to add code... – mavrosxristoforos Feb 25 '13 at 21:44
  • 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 Feb 25 '13 at 21:44
  • Thank you rocket, So I've been doing that wrong for ages... Well thank you so much! you've fixed my problem :) –  Feb 25 '13 at 21:55
  • Glad I could help! :-D – gen_Eric Feb 25 '13 at 21:57
  • so, just for clarity's sake, was the problem the incorrect usage of the `!=` , or the incorrect path? Or both? – Cashew Feb 26 '13 at 01:40
  • @Cashew: Sounds like it was both. – gen_Eric Feb 26 '13 at 15:46
  • It was actually only the != But I changed the path to rocket's way. :p –  Feb 27 '13 at 23:32

1 Answers1

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.

mavrosxristoforos
  • 3,573
  • 2
  • 25
  • 40
  • 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 Feb 25 '13 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. :) – mavrosxristoforos Feb 25 '13 at 21:38
  • Hrm, ok well now it says that it isn't writable... I set the CHMOD correctly. It should be working :/ –  Feb 25 '13 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 Feb 26 '13 at 01:36