PHP Force Download File in Browsers

Hi Guys, Sometimes we have to do it, reason can be any. We want user to download a file rather than opening it in browser. For that to happen browser have to know what to do. Here is a very useful code snippet that I use to force browsers to download file to disk rather than to open in browser for supported format. Now there are couple of ways to force download a file, first lets take a look at this code snippet Say you want a PDF file to force download Option 1 If you try to use above code snippet then the file test.pdf in my case does not open in my browser but infact it is downloaded. You can change the filename to anything you like. Approach 2 You can also do this as a global setting for any file you think should be available for download. You can use apache configuration to do this, either make change in your httpd.conf or add these lines to your .htaccess file   AddType application/octet-stream .avi AddType application/octet-stream .mpeg AddType application/octet-stream .mp4 AddType application/octet-stream .pdf AddType application/octet-stream .docx OR <FilesMatch "\.(avi|mov|mp3|doc|pdf)$"> ForceType application/octet-stream Header set Content-Disposition attachment </FilesMatch> You can add or remove file extensions to above list. This way you can control your force downloads globally on your site. Now thing is that second method should only be used when you are 100% sure that you want certain file types to be downloaded from your site. Else Approach 1 should be used. I generally use approach 1 because that way I've control on what file I want user to download. I hope this helps Cheers Advertisement     … [Read more...]

CPanel Apache error_log and access_log location and location for mySQL logs

Hi Guys, So we all know that CPanel makes our life easier in many ways. Guys like me who are not Linux power users but more of intermediate level users love CPanel. I do! Well there is a strange overlapping when we have full root access to our VPS or Dedicated server. CPanel does help with many things and some things we like to do traditionally. For me error_log and access_log are one of those that I love to watch from my lovely shell. Now to get to the location of your log file you can do few things. You can get your access log location from apache configuration for each domain, but let me give you pointers to where these files are located Apache access_log files Complete apache access log can be found here You can use command like tail to print last few lines of access log. Apache Domain specific access_log file If your server handles multiple domain requests then most likely they all have their own individual access_log files these can be located here Once again you can use tail command to check out last few lines in domain specific log file Apache error_log These log are more frequently visited than the access_log ones, one reason is that all your ERRORs and Exceptions that Apache came across are logged in this file. Thus we can find a problem and issue a fix. The location for error_log files is While fixing a problem I generally leave this command running to get the up2date log data mySQL Logs file with CPanel you can get hold of .err file here Configuration file can be located here     Apache Configuration File location httpd.conf can be found here You can viit like   I hope this helps Cheers Advertisement             … [Read more...]

Javascript Object Array sort by property

Hi Guys, I thought this would be a nice share just in case you are looking for information on how to sort Javascript Object arrays. As you may be aware that Javascript comes with a handy function called sort(function) You can read more about it here http://www.w3schools.com/jsref/jsref_sort.asp There are couple of things that this method is capable of. Either you can trust default sort or you can create your own function to sort. Let me show you couple of example to elaborate more on what I am saying. Alright consider this array Now if you use sort() method on the above array as shown below Result will be which is cool. What if you need to sort your array of object based on a property of an object. Well surely this approach wont work in that scenario. So what will work. Lets find out Consider the Object array as shown below Lets say I want to sort the above array in ascending order based on property value of Score Below is how thats done   Notice that I am using operator (-) you can also use (>) to get the same output that is Ascending Sort. Now if you try to output myObjArray to Javascript console then you will get below output   Advertisement   If you want to sort this Object array based on Descending value of Score then either change the operator to objA.Score < objB.Score as shown below I am not doing to go in details how sort works but the idea was to show how to utilize Javascript's internal functions to sort object arrays. I hope this helps Cheers,   … [Read more...]

Javascript array count method

Hi Guys, So for the project I am working on I am using both PHP and Javascript. Now I got a habit of using count() method in PHP for array to count the number of element in an Array. You can use length property of an Array in Javascript but I like it the PHP way so then I wrote these few lines of code to accomplish that So traditionally in PHP we use something like this PHP Pseudo Code This is A way to use count method but its used at many different places for different use cases. Now lets see whats the Javascript equivalent Advertisement   Javascript Count() method So say we have a Javascript array as shown below I can get the count of array elements by simply doing this Above will print 5 Thats good. I mean there is no real advantage of having a count() method but lets see how to do a count() method for an array. Now just return a length property may just not be enough to get what you want (an actual count of elements) A much better approach would be the one below and the one that I will use. Above approach will handle cases like this one Now when you do this Above  will print 5 For me I don't think that length in Javascript is true length of an array. But in most cases it does the trick. You can do the same for extending default behaviour for any known Javascript class. I hope this helps Cheers  P.S. :  Its always a good idea to keep your custom methods in your standalone object rather to extend native objects. That's my opinion. Advertisement     … [Read more...]

Remote Desktop Small Resolution when Connecting to SBS, RWW Internet Explorer

Hi Guys, My friend had a problem where she was trying to connect to her work machine and was getting much smaller resolution for my destination computer. I for one spent so long trying to find a fix for this problem. Now there are couple of things that you may need to do depending on how you are connecting to your Remote Machine. I use Remote Web Workplace to connect to my computer at office. If you are unaware even knowingly what RWW is then you should read this http://en.wikipedia.org/wiki/Microsoft_Remote_Web_Workplace and http://technet.microsoft.com/en-us/library/cc707996(v=ws.10) Now lets jump on to what my friends problem was when she tried to connect using Internet Explorer Her IE Zoom level was set to 150% Is that a Problem. I should say yes. If you understand how zoom level works then yes thats a problem but anyways setting it back to 100% fixed the resolution story. Now that I have touched this topic let me show you how you can change the IE Zoom level On the top right corner check where that Gear Wheel is as shown below Now GoTo Zoom level Menu and change it to say 100% as shown below or try different setting that feels right to your eye     Now that I am on this topic why not also show you that remoting in traditionally using "RDP" also has setting for resolution as shown below This is Off topic but I thought I should chuck it in just so that you are aware that it exisits So GoTo Programs> Accessories > Remote Desktop Connection and click Options The small window will become a bit bigger and click on Display tab. Under Display Configuration  move the slider towards Largest value as shown below     Now GoTo General tab and click Save This is so that you don't have to make that change everytime :) I hope this helps if you were in similar boat as my friend was. Cheers,     … [Read more...]

ServiceStack adding to allowed File extensions

Hi Guys, We had a weird problem at work where our font files won't load. One of my colleague Dr. Overton who is working on web services using Service stack for one of our project showed me what the problem was. Now I am 80% web guy and 20% applications. So for once I was like ok its something to do with configurations. That turned out to be true By default these extensions are allowed   As you can see that AllowFileExtensions is just a Collection Object property (which we can add extensions to) under class EndPointHostConfig So within your AppHost.cs file  where you are creating EndpointHostConfig config object as shown below After this declaration just add required extensions. In our case we want to process few font files that won't work otherwise without these changes   Advertisement   Much thanks to Dr Overton to figure this out.   I hope this helps someone Cheers … [Read more...]

WordPress check if post or page or home page

Hi Guys, Some times while developing a plugin we need to check whether a feature can be enabled or disabled based on if a webpage is a Wordpress type: POST Wordpress type: PAGE Wordpress type: HOMEPAGE There are few ways you can check that but here are some core wordpress function that I use to find our the page type. Lets go through these one by one to see how we can utilize these functions Check if type is PAGE Wordpress codex supply a function called is_page() you can use this function to identify if a post is a page. So confusing right! but you get the idea Here is a quick usage of this function above code will print 'page' on the screen if its a page For more information please check this link out http://codex.wordpress.org/Function_Reference/is_page   Check if type is POST Alright then we need to check this too many a times. Wordpress codex supply a function called is_single() to identify if post type is POST here is a quick usage to test it out Above will print post if you are browsing a post. More information can be found here http://codex.wordpress.org/Function_Reference/is_single by the way is_post() has been deprecated and should not be used. Check if type is HOME PAGE/Front page This is a common occurrence with many plugins trying to do some really specific things on home page. Such as displaying ads etc. Wordpress supply two functions called is_front_page() and is_home() Here is a quick usage on that.   Both of the above function will print home page when tested is_front_page is based on Settings->Reading->Front page displays setting, if it is is set to "Your latest posts", or when its set to "A static page" or "Front Page" value and the current Page being displayed is same as selected page this function returns true. is_home on the other hand is something I would use. To know more about these functions please … [Read more...]

WordPress copy paste protection blocker plugin

Hi Guys, When I wrote my previous post about disabling the copy paste with jQuery, I thought may be its going to be a good idea to just build a plugin for Wordpress too. So I just did that and its available from wordpress Its called jcwp copy paste blocker http://wordpress.org/extend/plugins/jcwp-copy-paste-blocker/ Screenshots Above screenshot shows the options that can be configured Below is a screenshot that shows it in use. Most of the stuff is silently done, it just when you enable Alert this happens   Options Disable content selection: When this is enabled then visitors on your website will no longer able to select any text as long as javascript is enabled and they don't use other means to copy content CSS disable text selection Styles below are added to the document to disable text selection. Keyboard events You can  disable keyboard events such as ctrl+save and ctrl+select all Disable right mouse click You can disable context menu as well Protection level You can choose if you would like to protect the pages, posts, homepage or everything Alert  Just as you  see in the screenshot above Alert message Custom alert message Powered by link Enable it to show your support Installation Upload unzipped plugin directory to the /wp-content/plugins/ directory Activate the plugin through the 'Plugins' menu in WordPress Use the Plugin Options on left menu under Settings > JCWP copy paste blocker. Works on Plugin has been tested on wordpress version 3.4.1 but it should work on wordpress version 2.8 onwards without any problems Browser support Plugin has been tested on IE8+ Firefox Chrome Safari Opera has not been tested on so I am not sure if that matters given the market share. Wanna support me? No one cares, but its worth asking. Bug Report Use comments to report a bug, Please mention your environment e.g.  Browser type, version etc. I hope that this plugin … [Read more...]

jQuery plugin for disabling right mouse click, text selection and document save

Hi Guys, Sometimes out content is really precious and we want to provide some basic protection against the content hackers. Now nothing in the web of world can be protected 100% because there are many ways to get your page content anyway. But this plugin gives basic protection against Text Copy Document save It handles mouse and keyboard events perfectly to give you that sense of security. To get hold of your content, someone will have to do a little bit more than just select, copy and paste. I will also through a demo at the end of this page but let me show you what this plugin is all about first. You will have granular control on what a plugin should do or what it should do, like say for example you can tell the plugin to Block right mouse click Or Block Text selection on your page Or to Block Text selection by keyboard. I named this plugin as jctextcopyprotector, sounds cool, right! :) not that much I would assume. Alright here is the plugin code Fork it on GitHub https://github.com/jaschahal/jctextcopyprotector The Plugin Code Alright lets discuss the settings properties Usage Stick this code somewhere on your page Options blockRightClick:true|false, This option if set to true will block context menu to appear blockDocTextSelection:true|false, This option if set to true will disable text selection using mouse useCSS:true, This option if set to true will use CSS to block text selection on your document blockPageSave:true, This option if set to true will block keyboard event such as Ctrl+A, Ctrl+S , Ctrl+C alertUser:false, This option if set to true will show an alert message when a blocked event is detected. alertMessage:'' Custom alert message Alert message on Right Click on one of my test pages looks like this   Alright so that's the test stuff. you can try it yourself from the link below The … [Read more...]

PHP time to days hours seconds function

Hi Guys, I thought I should share this useful code snippet with you that will convert time in seconds to Days Hours Minutes Seconds format Here is the function You can easily extend this function to include year/month/weeks information if you like. Let me show you the way you can use this function Code below will result in output 1 Day Code below will result in output 1 Day and 10 seconds will result in output 1 hour 34 minutes and 8 seconds I hope that this code snippet will be useful to you. If you end up modifying this code please share it using comments so that others can benefit. Cheers Advertisement     … [Read more...]