Jump to content

dalawh

Members
  • Posts

    127
  • Joined

  • Last visited

Everything posted by dalawh

  1. So would using header(location:) work? It does redirect and it will access the site again. Any other ways?
  2. Yea, I saw it. Must of posted before I refreshed. Oh okay. Is there a way where I could set the cookie and then use the cookies? I can't do it in the same script for obvious reasons. If I require or include another script, it will be considered as the same script. So is there a way to make one script occur and then the other one automatically?
  3. <?php$first="Booga";$last="Wooga"; setcookie("firstName",$first,time()+2592000); //30 days setcookie("lastName",$last,time()+2592000); //30 days $num=0; while($num!=100){ echo 'First: '.$_COOKIE['firstName'].'<br />'; echo 'Last: '.$_COOKIE['lastName'].'<br />'; echo '----------<br />'; $num++; }?> Every echo will give an undefined index. If you run another script after echoing the cookies, it will display it. That was just a hypothesis I came up with when I tested it and I wanted to confirm it.
  4. dalawh

    Writing into a file

    Oh okay. Thanks for the help.
  5. Does setcookie work after the script stops? When I make my script print the cookie after I set it, I get a notice saying it is undefined. If I run another script to print the cookies after the script that sets it, it works fine.
  6. dalawh

    Writing into a file

    The echo prints the string onto the page. If you want nothing printed, could I just do the below? if (file_put_contents($fileName, $data) !== FALSE){ } else{ } The above is the exact same as the below. file_put_contents($fileName, $data); If they are the same, wouldn't using the second one be better? Basically, there is no point of checking since checking or not, it won't do much more than print a statement.
  7. dalawh

    Writing into a file

    How would I check file_put_contents? If I use it, I am already calling the function. What I mean is if I check it with and if statement, wouldn't I be calling it and the contents will be put into the file? If that is the case, won't using the file_put_contents inside the if statement just cause it to put the content in the file again. I already tried to put the file_put_contents inside the if statement and I got an error. Basically I have no idea how to check and I was asking you how I would go about doing it.
  8. dalawh

    Writing into a file

    Checking it like I did in fopen won't cause the errors that will mess with the rest of the script. Since I can't check file_put/get and it causes an error, it will potentially mess with the script, which is something I do not want. if(file_exists($fileName)){ //File exists $prev=file_get_contents($fileName); file_put_contents($fileName,$data,FILE_APPEND); //Writes 'last | first | IP | date and time | page' into firstVisitLog.txt } else{ //File does not exists file_put_contents($fileName,$data); //Writes 'last | first | IP | date and time | page' into firstVisitLog.txt } Since I am not checking, the above code will work and it seems much simpler than the fwrite version. What do you think? Am I forgetting something important?
  9. dalawh

    Writing into a file

    So Called told me it is always good to check. Using fwrite without checking if fopen workd correctly could cause an error when it doesn't open successfully. Basically what I am asking is whether file_put/get will cause errors if the file was not open successfully. I know it will return FALSE, but will I get an error?
  10. dalawh

    Writing into a file

    I am trying to figure out if it opened successfully. In fwrite, I used "if($file)", but it doesn't work the same way for file_put/get.
  11. dalawh

    Writing into a file

    Using file_get_contents and file_put_contents seems to make the code much cleaner and easier to read. if(file_exists($fileName)){ //File exists $prev=file_get_contents($fileName); if(CONDITION){ //File opens successfully file_put_contents($fileName,$data,FILE_APPEND); //Writes 'last | first | IP | date and time | page' into firstVisitLog.txt } } else{ //File does not exists if(CONDITION){ //File opens successfully file_put_contents($fileName,$data); //Writes 'last | first | IP | date and time | page' into firstVisitLog.txt } } I am still unsure of what to fill in for the CONDITION to check whether the file was opened successfully. f I used "if(file_put_contents($fileName, $data)!=FALSE){...}". Wouldn't that make it already write in the file and doing it inside the if statement will cause it to write it again? I tried this and it seems to have given me an error. So this will not work. Oh, okay.
  12. dalawh

    Writing into a file

    Seems like file_put_contents is so much better than fwrite if you are using it to keep data in a file. If I use file_get_contents and file_put_contents, how would I check if the file was opened successfully that way I won't get thrown an error? When using fopen, fwrite, fclose, I can use... $file=fopen($fileName,"a+"); if($file){...} //<-- Check
  13. dalawh

    Writing into a file

    So fopen, fwrite, and fclose is for multiple data. What I mean is that you can use fwrite multiple of times without erasing the previous content. file_put-contents is like a single fwrite, so every time it is used, it erases the previous data, unless you append. Am I right? Which is more convenient, faster, safer, etc?
  14. dalawh

    Writing into a file

    Too bad the file_put_contents erases everything instead of adds to it. I just wrote this. Everything seems to work fine. Tell me what you think of it. $fileName='firstVisitLog.txt'; require 'date.php'; $data=$last.' | '.$first.' | '.getIP().' | '.getDateInfo().' | '.$_SERVER['SCRIPT_FILENAME']."\r\n"; if(file_exists($fileName)){ //File exists $prev=file_get_contents($fileName); $file=fopen($fileName,"r+"); //Writes to beginning if($file){ //File opens successfully //Writes 'last | first | IP | date and time | page' into firstVisitLog.txt fwrite($file, $data."\r\n".$prev); fclose($file); } } else{ //File does not exists $file=fopen($fileName,"a+"); //Creates and writes into it. if($file){ //File opens successfully //Writes 'last | first | IP | date and time | page' into firstVisitLog.txt fwrite($file, $data); fclose($file); } } Is there a better way to do this? I checked to make sure the file was opened correctly before writing into it like so called said. To be honest... they need to add a fwrite options where none of the contents gets deleted. I am sure many people use them in different fopen and they don't want their data deleted. Oh well.
  15. dalawh

    Writing into a file

    Yea, I will go write something and test it out.
  16. dalawh

    Writing into a file

    Oh. We must of misread it. Instead of using "a" to write to the end, can't I used "w" to create the text file and every case after the first, use "r+" to write to the top? I assume there is no option where you can create and write to the top right? Opening a file is not always guaranteed? If it doesn't open, should I use a recursive method to repeat itself (as in open and write)?
  17. dalawh

    Writing into a file

    It thought it would be easier reading the most current at the top than the bottom, but if it is not possible, I will do that. I looked at http://www.php.net/manual/en/function.fopen.php and it seems "c" should do the job. jsg confirmed it did too.It says if the file does not exist, it creates it. If the file exists, it does no truncate it and it points to the beginning. Am I reading it wrong? Um... here is the simple version.<?php$file=fopen("random.txt","c");fwrite($file,"BOO");fclose($file);?> Ignore the syntax if anything went wrong. I just wrote it up quickly. That is unlikely until it the site gets popular, but it is something to worry about. When you say no guaranteed results, do you mean it won't write or it won't write correctly or one of them is ignored? Is there any way to deal with this? I am using XAMPP to test things out right now. Once I am done, I will upload it to a file hosting site and hopefully test it out. I will switch over MySQL database later. I want to practice PHP right now to get a better hang of it.
  18. dalawh

    Writing into a file

    I am trying to keep track of the IPs of all visitors, so I am opening, writing, closing, opening, writing, closing, etc. I want to keep all the content, so I want it to write to the top of the text file. It says "c" does not truncate, so it should not delete. Am I right? It also says the pointer is positioned at the beginning.
  19. dalawh

    Writing into a file

    Not sure what is wrong, it says it does not truncate, but it seems like it does. Every time I use fwrite inside the same fopen, it writes after it, instead of above it. Every time I use fwrite to a different fopen, it cleans it and writes.
  20. Oh okay. require_once isn't even necessary for this case. I get an error when I use multiple require of the same file.
  21. dalawh

    Writing into a file

    Finally got a chance to use "c" and I noticed it writes over instead of adding to the top. Did I read the manual wrong?
  22. Yea. Now I am waiting on the mods since they are the ones who usually help me.
  23. I removed the colors Let's say that in test.php, I require date.php and I use getDateInfo(). After that, I require misc.php and use funcOne() and funcTwo().In date.php, I have the function, getDateInfo().In misc.php, I have two functions: funcOne() and funcTwo(). In funcOne() and funcTwo(), I have getDateInfo().My question is, do I have to require date.php inside funcOne() and funcTwo() to use the getDateInfo() or it isn't required because I already called it in test.php?
  24. Hope this isn't too complicated to understand. I even color coded it Let's say that in test.php, I require date.php and I use getDateInfo(). After that, I require misc.php and use funcOne() and funcTwo().In date.php, I have the function, getDateInfo().In misc.php, I have two functions: funcOne() and funcTwo(). In funcOne() and funcTwo(), I have getDateInfo(). My question is, do I have to require date.php inside funcOne() and funcTwo() to use the getDateInfo() or it isn't required because I already called it in test.php?
  25. Yup I wish notepad++ had something built in that tells me that that function was already declared, but I guess I should of figured it from the error I got.
×
×
  • Create New...