I am creating website in which a user can download .vcf file on its mobile phone.The file is not stored on my browser, but is generated using the stored data in my database.
Suppose I have a file abc.php:
<?php session_start();
?>
<?php
if($_SESSION[abc] != some_value)
{
echo "not ok";
exit();
}
else
{
header("Content-Description: File Transfer");
header("Content-Type: text/vcard");
header("Content-Disposition: attachment; filename=contact.vcf");
echo "All vcard details in the standard vCard format......
.......
.....";
session_destroy();
exit();
}
Now, this working perfectly fine a computer browser(IE,chrome,etc.) but when I try to do same thing from a mobile browser(android,windows phone), it does not work.
Sometime it creates a file abc.html(abc being the name of my php file) with the content being "not ok" or sometimes it creates a file contact.vcf/contact.html with content "not ok".(where "not ok" is the line I print when a session variable is not equal to some value)
I cant understand why it is working in a computer but not in smartphone. Please help. If it does not work from a mobile browser then the whole idea of my website is lost! thanks!
EDIT:
I have somehow solved the issue partially.Just before exit() in my loop , i was using session_destroy(); Now, i don't know why, but after everything is done, the control was again going to the top,check the "if" condition, and since now the session was destroyed, the condition was getting true, and i was getting "not ok" as output in my file.
now when i remove session_destroy(), it works fine(in mobile phones also).
Any ideas why the control is goin back to the top?
Looks like you are not setting up proper headers. Try this in PHP
header('Content-Description: File Transfer');
header('Content-Type: text/vcard');
header('Content-Disposition: attachment; filename=contact.vcf');
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
header('Content-Length: ' . $length); //Set content length here
ob_clean();
flush();
echo $content; //echo the content
exit;