⚠ In case you've missed it, we have migrated to our new website, with a brand new forum. For more details about the migration you can read our blog post for website migration. This is an archived forum. ⚠

  •     

profile picture

download helper



casachit
  • profile picture
  • Member

Posted 15 August 2012 - 11:20 AM

I know codeigniter has great download helper but I want something more than this. I want only logged in user be able to download file.. even if user knows the url .. he /she shouldn't be able to download file unless logged in... how can i do that??

web-johnny
  • profile picture
  • Administrator
  • 1,166 posts

Posted 15 August 2012 - 18:41 PM

Let's say that you have the method at your Controller Main:

function download_file($file){
$data = file_get_contents("/path/to/photo.jpg"); // Read the file's contents

$name = 'myphoto.jpg';
force_download($name, $data);
}

and let's say that you have the function [i]UserIsLoggedIn()[/i] that just returns true or false. So when the user is logged in the file then it will download it normally but if not then it will redirect you to the login page. So this can simply happens like this:

function download_file($file){
if(UserIsLoggedIn()){
if(!file_exists("/path/to/".$file))
{
show_error("Sorry my friend but this file doesn't exist.");
}
$data = file_get_contents("/path/to/".$file); // Read the file's contents
force_download($file, $data);
}
else
{
redirect('admin/login');
}
}


So the URL can easily be like this: [i]example.com/main/download_file/test.pdf[/i] and you can have it as a simple anchor:

<a href="http://example.com/main/download_file/test.pdf">download file here</a>

So if the user is not logged in you will simply redirect him to the login page. If not the user will download the file normally.

casachit
  • profile picture
  • Member

Posted 17 August 2012 - 10:55 AM

thanks web-johhny, I figure out that there was problem in my authentication code..... :)

there is one more help needed... My session is disappearing quicker.. Sometimes as soon as I logged in... can't figure out where's the problem... I'm not using ci_session.. here's my session library.. But the problem is only on xampp.... On server its working fine...but I dun want to take risk

<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
session_start();
class MY_Session extends CI_Session
{
function __construct()
{
parent::__construct();
}

function userdata($item)
{
if(empty($_SESSION[$item]))
{
return FALSE;
}
else
{
return $_SESSION[$item];
}
}

function set_userdata($items, $value = NULL)
{
if(is_array($items))
{
foreach($items as $item => $value)
{
$_SESSION[$item] = $value;
}
}
else
{
$_SESSION[$items] = $value;
}
}

function unset_userdata($items)
{
if(is_array($items))
{
foreach($items as $item => $value)
{
unset($_SESSION[$item]);
}
}
else
{
unset($_SESSION[$items]);
}
}

function sess_destroy()
{
session_destroy();
}
}
?>


can you provide me idea how to add remember me functionality?? I searched in codeigniter forum but failed to find proper solution..

goFrendiAsgard
  • profile picture
  • Member

Posted 23 August 2012 - 01:11 AM

Try to look for php.ini in your xampp directory.
Then, try to modify this: http://www.php.net/manual/en/session.configuration.php#ini.session.cookie-lifetime