Thursday, July 30, 2015

Add quotes to values in a string separated by a comma php

I have a comma seprated string that can contain multiple values like charecter and number or any thing else. Now i am using this with the IN clause in sql query.

Let see example :

$str = "k, w, 1, 2, 3, 4, 5, 6, 7, 8";

Now i am running this with 'IN' clause in sql query like below :

QUERY = "SELECT * FROM tbl_user WHERE grade IN($str)";

When i run the above query its threw error because in my string k and w are charecter not a integer so its threw error. If i add a single inverted comma before and after each of the integer value then problem will be resolve. Like below :

$str = "'k', 'w', '1', '2', '3', '4', '5', '6', '7', '8'";

Solution : To do this in php you have to perform a simple sting replacement like below -

$str = "k, w, 1, 2, 3, 4, 5, 6, 7, 8";
$str =" ' ".  str_replace(",", "','", $str) ." ' ";
echo $str ;

OUTPUT :  It will return the string like this - 'k', 'w', '1', '2', '3', '4', '5', '6', '7', '8'
 
 Chears
Happy coding :)

Wednesday, July 29, 2015

How to prevent a file from DIRECT URL ACCESS?

I have a project on my server and there are some confidential PDF and SWF extention files which is load from our website. But i don't want that any one those are not the access of our website can't be able to access these confidetial data by just copy and paste the link url on address bar. To avoid this security hook you need to just write a code in your htaccess file.

Below is the code of htaccess :-

<IfModule mod_rewrite.c>
    RewriteEngine on   
    RewriteCond %{HTTP_REFERER}       !^http://(www\.)?yourDomainName/ [NC]
    RewriteCond %{HTTP_REFERER} !^http://(www\.)?yourDomainName.*$ [NC]
    RewriteRule \.(swf|pdf)$ - [F,NC]
</IfModule>



Changes in code : You just need to copy the above code and put it into a file. After that change bold highlited text "yourDomainName" with your website domain name. Suppose your website name is 'www.yahoo.com' then here yahoo is your domain name so you have to replace yahoo with the 'yourDomainName' into the code. After that save this file with the name '.htaccess'. After creating this htacess file put it on the root of your website folder.

Testing your code : When you are testing the code don't forget to clear the catche of your browser every time.
Now try to access any pdf or swf file by direct link on browser. It will show a permission message of unauthorize access.
Like below screne shot-

Prevent a file from DIRECT URL ACCESS
Happy coding :)
Related Posts Plugin for WordPress, Blogger...