Nov 18
Basically downloads are normal direct link to real files in the server with a complete static url link to the file. So any body can click the link and can download the file. Any cross site script can access the file any time or any user can access the file from anywhere.
What if you want to allow file downloads only if the user is logged in.
what if you want to hide the actual file and its folder from displaying it to the user instead you want to show a different url which could probably be a server side script file (.php for example). and that could fetch the actual file with a different name.
For that i use the force download concept. I will just send the id of a file for which its filename and location are always hidden. take this for example…
<a href='sitename.com/files/files.php?id=45'>Click here to download</a>
and in files.php firstly i will check whether the users has logged in and only then i will let the file to download… else nothing happens.
here is a sample code to do a force download.
//select fn from tablename where id=$_request[id]
//assume that the file is in the junk named folder
// the the force download script will look like the following
$filename = "doc_O1jtIYi4jkg8Xh2k/$fn";
header("Cache-Control: no-store");
header("Expires: 0");
header("Content-Type: application/octet-stream");
header("Content-disposition: attachment; filename=\"".basename($filename)."\"");
header("Content-Transfer-Encoding: binary");
header('Content-Length: '. filesize($filename));
readfile($filename);
the readfile function reads the contents of a file and outputs to the client.
cache control no-store if for geko browsers and no-cache can be included for IE browsers
You can either use the exact mime type if you know in the place of content-type.
This works in all browsers. People either download, save and view the file or they directly open the file. When they do the second and if their browser is IE 6 then you get a message that ‘cannot access file from temporary internet folder’ so for IE 6 the users has to save the file first and then they have to open it.
If you find any sense if not appropriate then please post a comment.
Oct 02
You can prevent the flash showing the loading progress on each page by using the flashvars.
I wanted the menus in flash to be highlighted when the page is under that menu. for example i have home, about, services, contact. now i am in home page. when i click services page that page loads and i want services to be highlighted in a different color. so when ever you are in any page the menu for that page should be highlighted. for doing that i use.
<param name="movie" value="flash/header2_fv8.swf?button=0">
the number for button will vary for each menu. the button number is the reference for menus which are to be highlighted when a page is in view.
so on any page home can be button 0 and so on… and in html of any page what ever button number you give in object tag that menu in the flash is highlighted…
if you use a query string directly like
<object data="flash/header2_fv8.swf?button=0">
or
like
<param name="movie" value="flash/header2_fv8.swf?button=0">
then for every page load
the flash movie will show the loading progress.
The reason might be that every time you send a query string and flash could assume it as a
new url and it loads as if it is loading for the first time. just an assumption. and some
people too said so.
so to prevent this that is to prevent flash to show the loading progress again and again
you can use the FlashVars attribute like in the following.
<param name="movie" value="flash/header2_fv8.swf?button=0">
here is a sample code
<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,24" width="980" height="394">
<param name="movie" value="flash/header2_fv8.swf" />
<param name="quality" value="high" />
<param name="menu" value="false" />
<param name="wmode" value="transparent" />
<param name="FlashVars" value="button=1">
<!--[if !IE]> <-->
<object data="flash/header2_fv8.swf" width="980" height="394" type="application/x-shockwave-flash">
<param name="quality" value="high" />
<param name="FlashVars" value="button=1">
<param name="menu" value="false" />
<param name="pluginurl" value="http://www.macromedia.com/go/getflashplayer" />
<param name="wmode" value="transparent" />
</object>
<!--> <![endif]-->
</object>
…
…
Aug 08
Warning: Unknown: Your script possibly relies on a session
side-effect which existed until PHP 4.2.3. Please be advised that
the session extension does not consider global variables as a
source of data, unless register_globals is enabled. You can disable
this functionality and this warning by setting
session.bug_compat_42 or session.bug_compat_warn to off,
respectively. in Unknown on line 0
This error likely appears when setting some thing like this $_SESSION['something'] = NULL; or you are assigning a variables value to a session which is null. hopefully.
what people say is better turn off this warning by
1. "session.bug_compat_42 = 0" in your php.ini
2. setting the following in your .htaccess file
php_flag session.bug_compat_42 0
php_flag session.bug_compat_warn 0
3. Directly write the following code in your php script where you want to
stop the warning. It is better if you set it in a common include file.
ini_set('session.bug_compat_42',0);
ini_set('session.bug_compat_warn',0);
...
…
Jul 24
Normally when we include flash html code it will have an object tag which will have a child
When we set html document type to strict then w3c validation shows error on embed tag. So what could be the alternate to include a flash code for w3c strict mode.
i found this code in google codes… and i have given the link below to the resource which is in detail and also has a java script alternate of including flash content in a html page.
<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" width="780" height="420">
<param name="movie" value="myContent.swf" />
<!--[if !IE]>-->
<object type="application/x-shockwave-flash" data="myContent.swf" width="780" height="420">
<!--<![endif]-->
<p>Alternative content</p>
<!--[if !IE]>-->
</object>
<!--<![endif]-->
</object>
the complete documentation can be found at the following url…
http://code.google.com/p/swfobject/wiki/documentation
…
Jul 21
Use the basic phpmailer code which comes with the phpmailer zip form phpclasses.org
the following code is the default one which sends mail from your server by using the default server settings.
the host name is set to localhost and authentication is false(which takes the default). these are the two lines which
vary when sending mail form localhost.
include("includes/phpmailer/class.phpmailer.php");
$mail = new phpmailer();
$mail->PluginDir = "/include/";
$mail->IsSMTP(); // send via SMTP
$mail->From = "noreplysample.com";
$mail->FromName = "From Name";
$mail->AddAddress("address@host.com");
$mail->WordWrap = 65; // set word wrap
$mail->IsHTML(true); // send as HTML
$mail->Subject = "Mail Subject";
$mail->SMTPAuth = false; // turn off SMTP authentication
$mail->Host = "localhost"; // SMTP servers
$mail->Body = $msg;
$mail->send();
the code to send mail form your localhost to any remote email server
the difference in the above and the following is i have given the important part which need to send mail from localhost
the first below two lines differ from the above. and the last two lines are added.
you have to give your servers auth information to send mail. So when ever mail is sent from localhost to a remote server
a server reference is needed. which means… mail is sent form localhost on-behalf of your server.
$mail->SMTPAuth = true; // turn on SMTP authentication
$mail->Host = "samplesite.com"; // SMTP servers
$mail->Userame = "testing@testing.com";
$mail->Password = "testing";
so when the above lines gets executed this code connects to your site for verification
and then the mail is sent with reference to your server. so
you need a server by using which you can send mails from localhost.
here is the full code which sends mail from localhost.
include("includes/phpmailer/class.phpmailer.php");
$mail = new phpmailer();
$mail->PluginDir = "/include/";
$mail->IsSMTP(); // send via SMTP
$mail->From = "noreplysample.com";
$mail->FromName = "From Name";
$mail->AddAddress("address@host.com");
$mail->WordWrap = 65; // set word wrap
$mail->IsHTML(true); // send as HTML
$mail->Subject = "Mail Subject";
$mail->SMTPAuth = true;
$mail->Host = "samplesite.com"; // SMTP servers
$mail->Userame = "testing@samplesite.com";
$mail->Password = "testing";
$mail->Body = $msg;
$mail->send();
the from address should be a valid domain string. else mostly your mail will not be sent.
…
Jul 21
by default we have our files stored in a user defined path like d:\www\xampp\htdocs\site1
when we access we give as localhost/site1
using virtual host option you can use like the following
consider you have your site in d:\www\xampp\htdocs\site2
you can access your site as “http://site2″ or “http://site2.com” or “http://site2.local” … by updating the httpd.conf file
which will be present in the apache/conf folder of your installation.
edit your httpd.conf file and go to the bottom of it. and you can add like the following.
i assume that your default document root is d:\www\xampp\htdocs
NameVirtualHost 127.0.0.1
<VirtualHost 127.0.0.1>
DocumentRoot "d:\www\xampp\htdocs\site1"
ServerName site1
</VirtualHost>
before using this you have to do two steps
one is restarting apache and the other is as follows
since we are going to give http://site1 windows will connect to the internet to fetch the site but we have to inform windows
not to connect to the www and instead connect to the local site1. for this you have to add the site1 in a windows file which
is stored in the following folder
C:\WINDOWS\system32\drivers\etc
the file name is “hosts” without extension
you have to add the following line in the file “hosts”
127.0.0.1 site1 or
127.0.0.1 site1.com [if you want to access that way] or any of your wish
consider that you are having your site outside the default path of apache say d:\mysite
then you have to follow the steps below to include that
<Directory "D:\mysite">
Order Deny,Allow
Allow from all
</Directory>
and this besides the similar code
<VirtualHost 127.0.0.1>
DocumentRoot "d:\mysite"
ServerName mysite
</VirtualHost>
include the above better before NameVirtualHost 127.0.0.1
so you have two sites now and you can run your sites like the following
type in the address bar after restarting apache as “site1″ which will automatically be called as “http://site1″ and mysite whic will be like “http://mysite”
try it and enjoy…
Jul 21
If you are using strict mode as your html document type and if you have target=_blank in your html code then wc3 gives you an error. strict mode does not accept target=_blank and they suggest to use transitional mode.
here is the javascript replacement for target=_blank. before this you have to set the rel attribute of anchor tags to “external” or anything you wish because this is used in the javascript code so see which are all the anchor tags has set the rel attribute to external .
in other words you can set the rel attribute with some value to mean that these are the tags which has to be opened in a new window.
<script type="text/javascript">
function target_blank() {
var anchors = document.getElementsByTagName("a");
for (var i=0; i<anchors.length; i++)
{
if ((anchor[i]href!='') && anchor[i].rel == "external")
anchor.target = "_blank";
}
}
window.onload = traget_blank;
</script>
here is a script with which you can make a single anchor tag open in new window using javascript
<a href="http://host/path.html"
onclick="window.open(this.href); return false;"
onkeypress="window.open(this.href); return false;">
Open in new window using javascript</a>
…
Jul 21
To remove windows messenger from loading when outlook 6 starts follow these steps.
start - run - in the box type gpedit.msc.
A window will appear
in that select
local computer policy - Administrative Templates - windows components - Windows Messenger
In the right pane you will have two options and you can double click them and choose enable.
Start outlook. go to tools - general and disable messenger option from auto logging if present.
this is for windows messenger and not for msn messenger.
Jul 17
static variables in javascript
In javascript functions and objects work in the same manner. Functions are also objects.
so a function can have a member variable like in objects.
A member variable will retain its value between function calls.
So this aspect is used for having a static variable in javascript.
Actually there is no option like static variables in javascript. so here the member variable server as a static variable in javascript.
here is one simple script which uses a static variable to maintain a counter
<script>
function addmore()
{
if(!addmore.counter)addmore.counter = 0;
addmore.counter++;
alert(addmore.counter)
}
addmore();
addmore();
</script>
or here is another version
<script>
function addmore()
{
if(!this.counter)this.counter = 0;
this.counter++;
alert(this.counter)
}
addmore();
addmore();
</script>
i have used this for multiple file uploads where there will be an addmore button and a variable to maintain the count of number of uploads to display and once this.counter is greater than 10 then i alert a message that uploads cannot be more than 10.
some people had an idea that why done we use like the following instead of using it like a member function
<script>
var counter=0
function addmore()
{
counter++;
alert(counter);
}
addmore();
addmore();
</script>
my answer is not to mess up the code with too many declarations which uses the word var…
after a long time we would wonder which variable is for which function
and if the variable is with respect to the context of the process then that will a good practice of maintaing standards and more meaningful.
if you would like to suggest an alternate please comment on this because i would like to know more if there is anything beyond my scope on this.
Jul 07
php code to replace the width and height of any html tag using php and regular expression.
i had a youtube listing for the admin where i display 10 per page but usually the object tag width and height will be around 400×350 approximately.
but i want to show the youtube video small in the admin section so that it will match the size of each row which will have edit, delete buttons…
here i wished the width and height to be around 100×80.
so when i display the youtube object tag code from the data base i used the following regular expression to replace the original width and height with my preferred value.
and here is the code to do the width and height replacement. it works for me and hope for you too…
$pattern = "/height=\"[0-9]*\"/";
$string = preg_replace($pattern, "height='120'", $rs['url']);
$pattern = "/width=\"[0-9]*\"/";
$string = preg_replace($pattern, "width='200'", $string);
echo $string;
we have another alternate for the above …
you can write the above code in one line by using alternation
$pattern = '/(width|height)="[0-9]*"/i';
//
you can use single-quotes instead of double-quotes to reduce the need to escape characters like double-quotes and backslashes.
the input was like this…
<object width="425" height="344">
<param name="movie" value="http://www.youtube.com/v/GwQMnpUsj8I&hl=en&fs=1">
</param><param name="allowFullScreen" value="true">
</param><param name="allowscriptaccess" value="always">
</param><embed src=http://www.youtube.com/v/GwQMnpUsj8I&hl=en&fs=1
type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="425" height="344">
</embed></object>
and the output will be like this…
<object width="200" height="120">
<param name="movie" value="http://www.youtube.com/v/GwQMnpUsj8I&hl=en&fs=1">
</param><param name="allowFullScreen" value="true">
</param><param name="allowscriptaccess" value="always">
</param><embed src=http://www.youtube.com/v/GwQMnpUsj8I&hl=en&fs=1
type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="200" height="120">
</embed></object>
there are two width and height attributes which got replaced… one is in the first line and next is for the embed tab inside.
…
Recent Comments