Jump to content

via Ad Packs

download helper


  • Please log in to reply
3 replies to this topic

#1 casachit

casachit

    Advanced Member

  • Members
  • PipPipPip
  • 33 posts

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??

#2 web-johnny

web-johnny

    grocery CRUD Author

  • Administrators
  • 916 posts
  • LocationLondon

Posted 15 August 2012 - 06: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 UserIsLoggedIn() 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: example.com/main/download_file/test.pdf 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.
Posted Image

#3 casachit

casachit

    Advanced Member

  • Members
  • PipPipPip
  • 33 posts

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..

#4 goFrendiAsgard

goFrendiAsgard

    Advanced Member

  • Members
  • PipPipPip
  • 137 posts

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/m...cookie-lifetime

No-CMS homepage : http://www.getnocms.com
No-CMS forum : http://www.getnocms.com/forum
No-CMS Github page : https://github.com/g...diAsgard/No-CMS

 

Please don't mix up No-CMS and groceryCRUD issues.
(eg: user authentication etc is No-CMS specific issue and has nothing to do with groceryCRUD)

 





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users