file_put_contents() for PHP4!

July 30th, 2007 by Quatrux

When I was using PHP4 for my web stuff, I wrote a quite good function for usage with PHP4, as a lot of you know on PHP5 a very handy function file_put_contents(); exist, but not in PHP4, but if you want to use it on PHP4, due to your server still has it running, which in my opinion is still quite normal, I will share another of my functions, I just browsed around my sources which usually never is finished and found it, so thought to share, it is in my opinion quite different when the one offered on the PEAR package, but it does the same, just is a little bit expanded.. I will explain a little! Firstly I named it put_file_contents(); to avoid the risk of duplication, but for instance, just do a check with the simple function function_exists(); Moreover, I recommend to include your functions only once, I mean use include_once() and/or require_once() ;)

  1. function put_file_contents($file, $data, $mode = ‘a’, $path = ‘0′, $y = ) {
  2.         /* check if file exists and atempt to Create it */
  3.         if (!file_exists($file) && !touch($file)) {
  4.                 user_error(‘put_file_contents() Can not create the file’, E_USER_WARNING);
  5.                 return FALSE;
  6.         /* check if Directory exists */
  7.         } elseif (is_dir($file)) {
  8.                 user_error(‘put_file_contents() Can not write to a directory’, E_USER_WARNING);
  9.                 return FALSE;
  10.         /* check if file is regular */
  11.         } elseif (is_file($file)) {
  12.                 /* check the Flags */
  13.                 if (!strpos(‘**|a|w|t|’, ‘|’.$mode.‘|’)) {
  14.                         $mode = ‘a’;
  15.                 /* Change Owner if required */
  16.                 } elseif ($mode == ‘t’ AND !qcreate_file($file)) {
  17.                         user_error(‘put_file_contents() Can not change file owner’, E_USER_WARNING);
  18.                         return FALSE;
  19.                 }
  20.                 /* check if file is writabl/stronge otherwise try to change permsissions */
  21.                 if (!is_writable($file) && (!chmod($file, 0666) OR !qftp_chmod($file))) {
  22.                         user_error(‘put_file_contents() Can not write to file, permission denied’, E_USER_WARNING);
  23.                         return FALSE;
  24.                 /* Change Data Array to String */
  25.                 } elseif (is_array($data)) {
  26.                         $data = implode($y, $data);
  27.                 /* Check Input Data */
  28.                 } elseif (!is_scalar($data)) {
  29.                         user_error(‘put_file_contents() The 2nd parameter should be either a string or an array’, E_USER_WARNING);
  30.                         return FALSE;
  31.                 }
  32.                 /* Open and Store Data to File */
  33.                 if (($fo = fopen($file, $mode, $path)) === FALSE) {
  34.                         user_error(‘put_file_contents() Failed to open stream: Unknown reason’, E_USER_WARNING);
  35.                         return FALSE;
  36.                 }
  37.                 if (fputs($fo, $data) === FALSE) {
  38.                         user_error(‘put_file_contents() Failed to write to file: Unknown reason’, E_USER_WARNING);
  39.                         return FALSE;
  40.                 }
  41.                 /* Close Handle */
  42.                 fclose($fo);
  43.                 return TRUE;
  44.         } else {
  45.                 user_error(‘put_file_contents() Can not continue: Not a file’, E_USER_WARNING);
  46.                 return FALSE;
  47.         }
  48. }

As you can see, several times more of my stupid custom functions were used, you can remove them, but I will still share them, even though I don’t think they are useful.. I just tried to paste them, but I saw even more custom things :D So I think I will only leave and idea and not an actually working code.

Posted in PHP | 1 Comment »

PHP Designer 2007

June 3rd, 2007 by Quatrux

I wrote to the author of this software, which now is commercial, but doesn’t cost a lot a feature I want to see in PHP Designer, because without it, it really annoyed me and I was searching for alternative PHP IDE and was unhappy, due to most of those free ones sucked and others were costing to much.. So in the version of 5.3.2 in the changelog I saw my wanted feature.. Before that it could open last opened file or no files or all recent opened files, but it sucked, I wanted to open all the files which were opened before I closed the program, so it now does it and all I can say is that PHP Designer 2007 is one of the best IDE for PHP!

Get PHP Designer 2007

Posted in MySQL, PHP, Windows | No Comments »

Want to delete a PHP Session?

March 15th, 2007 by Quatrux

A lot of PHP novice programmers which don’t want to understand how the sessions work in the beginning, usually doesn’t delete a session the right way, so I wanted to post an example PHP script session_delete(); which deletes everything as needed.

  1. function session_delete($sname = ‘Current User’) {
  2.         # Set Session Name to a Variable
  3.         $name = session_name();
  4.         /* Empty the Cookie from Session */
  5.         if (!headers_sent() ) { setcookie($name,"",0,"/"); }
  6.         /* Remove the Cookie Value */
  7.         unset($_COOKIE[$name]);
  8.         /* Remove all the Info from the Super Global */
  9.         $_SESSION = array();
  10.         /* Free all session variables */
  11.         session_unset();
  12.         /* Destroy all data registered to a session */
  13.         if (session_destroy() === FALSE) {
  14.                 return FALSE;
  15.         } else {
  16.                 return TRUE;
  17.         }       
  18. }

Posted in PHP | No Comments »

What is the difference between ["PHP_SELF"], ["SCRIPT_NAME"] and ["REQUEST_URI"]?

March 15th, 2007 by Quatrux

What is the difference between ‘PHP_SELF’, ‘SCRIPT_NAME’ and ‘REQUEST_URI’ in the superglobal $_SERVER[] or also known as $HTTP_SERVER_VARS which is deprecated since PHP 4.1.0 and isn’t really a superglobal, but anyway, what is the difference? On different support channels and forums people ask this question and I tried to search google and didn’t find an answer, so I thought to write it here.

$_SERVER['PHP_SELF'];

The filename of the currently executing script, relative to the document root. For instance, $_SERVER['PHP_SELF'] in a script at the address http://example.com/test.php/foo.bar would be /test.php/foo.bar.

  • http://example.com/ — – — /index.php
  • http://example.com/test/index.php — – — /test/index.php
  • http://example.com/index.php?q=submit — – — /index.php
  • http://example.com/index.php/test/ — – — /index.php/test

So actually it returns the path to the current filename typed in the url without the QUERY_STRING, but when we us index.php/test/download/ it shows the path to ../test/download/ as we want it to do that, because usually using PHP_SELF is very useful with submitting forms, so having the PATH_INFO included in the PHP_SELF is a good idea and if you don’t want it, just use SCRIPT_NAME which will always show the executing file.

$_SERVER['SCRIPT_NAME'];

Contains the current script’s path. This is useful for pages which need to point to themselves.

  • http://example.com/ — – — /index.php
  • http://example.com/test/index.php — – — /test/index.php
  • http://example.com/index.php?q=submit — – — /index.php
  • http://example.com/index.php/test/ — – — /index.php/

So it is similar to PHP_SELF, just the PATH_INFO isn’t included, so you just point to the current filename executing. Note; that SCRIPT_FILENAME actually is the same, but it also returns the server root and acts the same as $_SERVER['DOCUMENT_ROOT']; . ” . $_SERVER['SCRIPT_NAME'];

$_SERVER['REQUEST_URI'];

The URI which was given in order to access this page; for instance, ‘/index.html’.

A lot of people usually don’t understand the usage of this, for example if you wanted to access index.html but it didn’t exist on the server, you get redirected to say 404.php page which says that the filename doesn’t exist, when you will try to use any other value, like PHP_SELF or SCRIPT_NAME it will print you /404.php and not the file you requested, but when you will use REQUEST_URI, it will print the full URL which you typed in order to get to that page, this sometimes is quite useful.

In addition, I recommend to use the constant __FILE__ if you want to get the absolute path to your current dir and if you want to set an include path, it is best to do it also with this constant by adding the function dirname(); like this: dirname(__FILE__);

  1. /* Change Super Global */
  2. $s =& $_SERVER;
  3. /* Set the Default Include Path */
  4. set_include_path( dirname( $s[‘SCRIPT_FILENAME’] ).‘/includes’. PATH_SEPARATOR . dirname(__FILE__) );

If you ask if this is different, when I will say yes it is. The first value points to the currently executing file like /index.php and the second value is pointing to the current included file directory, it even can be outside /public_html/ somewhere in /home/user

Sometimes people don’t want to get the filename, but only the Query of the GET method, so there is a value $_SERVER['QUERY_STRING']; which returns everything after the ?

  • http://example.com// — – — (nothing)
  • http://example.com/test/index.php — – — (nothing)
  • http://example.com/index.php?q=submit — – — q=submit
  • http://example.com/index.php/test/ — – — (nothing)
  • http://example.com/index.php/test/?q=submit — – — q=submit

Furthermore, if you want to get the server root, you can use $_SERVER['DOCUMENT_ROOT']; which usually returns something like /home/user/public_html/

Moreover, sometimes you can have an headache if you want to get the domain the script is currently running on, you ask why? because sometimes you can use not the right $_SERVER array key for that.. if you’ll be using $_SERVER['SERVER_NAME']; you will get the domain of the server, but say you’re using CPanel and added a domain through it and you will execute under it, you will still get the original server domain, this is called virtual host, in order to get it you need to use $_SERVER['HTTP_HOST'];

Read more about Reserved Server Variables on PHP dot net Manual!

Posted in PHP | No Comments »

3 Column CSS layout

March 5th, 2007 by Quatrux

3 Column CSS layout can be made in different ways, but I wanted to make one with divs and not html tables.. the way I wanted to make it is: I need a left menu bar, a right sidebar and the middle content bar.. the hardest thing for me was to make it without any width with set pixels, this was hard, but I managed to create such a layout with display: table; table-row and table-cell, where I created three table-cells for it, the secret is that the sidebars are using white-space: nowrap; I need them to stretch and be as wide as the text there is, I don’t want the links to break to a new line, if I don’t break a line with br, same with the other sidebar, but that means that the middle bar automatically will need to shrink when the sidebars stretch, this is quite hard to do, using html tables it would be quite easy.. using css display: table; is also easy, so where is the problem? Internet Explorer, that is the fucking problem! It doesn’t support display: table; as it should, so I created another stylesheet for IE, where the left sidebar is floating to the left and the right bat is floating to the right, I didn’t set any width, they are as wide as text, but how the hell to put the middle bar to use all the middle left space? Well, on IE you can write expressions, javascript expressions, so I just using javascript in css file (all my layout width) – (left sidebar width + right sidebar width) and got the middle width, it doesn’t work if javascript is off, but people who browse with javascript turned off understand that they are not getting all the features they can, besides, if you have javascript off, you will see the same layout, just not using all the available width! oh yea, I forgot to mention, that the middle bar is also floating to the left with float: left; here is the expressions code:

  1. div#content {
  2.         float: left;
  3.         width: expression((774(document.getElementById(‘menu’).clientWidth + document.getElementById(’sidebar’).clientWidth)) + "px");
  4. }

So this solves the issue with IE, this is a little hack to make it work as with display: table; If I haven’t thought of this, I was ready to make pages of my site which browse with IE that IE sucks and suck a dick if you browse with it :D

Posted in Webmastering | No Comments »

Moved from tables to divs!

February 23rd, 2007 by Quatrux

Well, at last, I moved from table designs to divs, I sticked with tables, due to I was a guy who used to use them for old browser support, but as less and less people browse with IBrowse and AWeb, I am really working further and using CSS for designs now as all “normal” webdesigners.. But I found one thing why tables are better than divs, you don’t have such control over tables, but you can do anything with them and it is much easier to have a nowrap design with 3 columns than with css, with css it is impossible, you have a choice of fixed width or a fluid design, or elastic, but it doesn’t work as with tables.. So, when your text is longer and you don’t want to break the line, you have to live with it that the text will get over the sidebar or whatever and will look horrible.. so you don’t need to have such words and need to use the right way with font sizes and etc. ;) But using CSS divs you save space, pages load faster and it is easier to do PHP with them, especially in loops, but also with template engines, it is useless.. as someone said to me – I have to remember, that tables isn’t CSS, furthermore, I know even more hate the fucking IE! :P

Posted in Webmastering | No Comments »

PHP function scan_dir();

January 23rd, 2007 by Quatrux

As a lot of who knows,PHP5 has a very good function called scan dir, but ash many of use still like to use PHP4 we need to find aletrnatives for it so here is a good function I wrote years ago and want to share it with you :)

  1. /* #### SCAN DIR ####
  2. Input: Directory, 1 to rsort() and files to not index to the Array
  3. Output: makes an Array with filenames and dirs on Success or False
  4. Status: Complete
  5. Last Editor: Quatrux
  6. */
  7.  
  8. function scan_dir($dir, $s = ‘0′, $u = ) {
  9. /* Check if input is a Directory and Open it */
  10. if (is_dir($dir)) {
  11. if ($d[‘open’] = opendir($dir)) {
  12. $u = "**.|..|$u|";
  13. while (($d[‘file’] = readdir($d[‘open’])) !== false) {
  14. /* Don’t add unneeded Files */
  15. if (!strpos($u, $d[‘file’]) ) {
  16. /* Make an array with all left Files */
  17. $d[‘array’][] = $d[‘file’];
  18. }
  19. }
  20. closedir($d[‘open’]);
  21. } else {
  22. return FALSE;
  23. }
  24. } else {
  25. return FALSE;
  26. }
  27. /* Check if Directory is not Empty */
  28. if (!isset($d[‘array’])) {
  29. return FALSE;
  30. /* Sort the Array by Request */
  31. } elseif (is_array($d[‘array’])) {
  32. if ($s == ‘1′) {
  33. rsort($d[‘array’]);
  34. } else {
  35. sort($d[‘array’]);
  36. }
  37. }
  38. return $d[‘array’];
  39. }

Well, will need to find a plugin or something which would highlight the code or something, because now, I don’t even see tabs, oh well.. Stupid Javascript Editor for Wordpress ;)

Posted in PHP | No Comments »

To much Spam

January 6th, 2007 by Quatrux

Well, I loaded another simple plugin for Wordpress Blog, to avoid Spam, now everyone who wants to post a comment, needs to Enter a security Code, for example a word Spider or else he will just see an error message, in that way I will save some resources and Akismet won’t need to do so much work every time, the Security Code is almost random, so Fuck You! You fucking spammers and spam bots, I have an headache from you, but you know what, it is really fun to program in PHP and write different stuff to avoid you, you’re doing something Good too, life wouldn’t be so fun without you, darn idiots.

Posted in Personal, Webmastering | No Comments »

At Last Something Serious

May 8th, 2006 by Quatrux

So many years have past from the Time IH (InitialHost) went down, I always wanted to reopen something like Free Web Hosting and even offer more Services and now when I got a Server which can host quite a lot of people, the dream came true ? or the time has come ? I registered another domain, this time it is called dqzone.net and it stands for Dave and Quatrux Zone, Online Services including Free Web Hosting and Paid Web Hosting, which is really cheap and reliable. Today I finished making the layout for DQ Zone and we like it! Will need to write content for it and what I have never done before is making a skin for PHPBB, we decided to use PHPBB Software as our Support and Discussion Forum and I will try to make the skin look like the layout and even will install the forum in the root directory. So this is the first happy days, hope exams will be good and I will finish them and will have all the summer until the University to create stuff, I am fed up with alcohol, time to Take over the Web! ;)

Posted in Personal, Webmastering | No Comments »

Projects finished

May 7th, 2006 by Quatrux

Woot! I have finished the site CMS, now it is complete and I will only update it.. In fact it is the first project I finished completely, my sites can run on it easily, I will just need to write some plugins for extra work, planning to write Statistics, Guestbook, Comments plugins soon.. The three sites (Quatrux, Faustas and Chinchilla) are finished, so now I have planned to create some sites which in my opinion can make me some more unique visits, it is about Google Adsense and about Chuck Norris, yeah he is immortal :D Furthermore, planning to reopen some of my old sites, the Gallery and an IRC channel site with several pages and a shout box, has to be easy, but I lack time and am lazy so it will take some time, because I am also planning to translate all my content to Lithuanian, my mother tongue – it gets me extra visits.

Moreover, I am still planning to reopen the Q Zone network, but I it is still 5% finished, I need a lot of coding to do.

Posted in Personal, Webmastering | No Comments »

« Previous Entries