Current location: Hot Scripts Forums » Programming Languages » PHP » Creating multiple sessions in download script?


Creating multiple sessions in download script?

Reply
  #1 (permalink)  
Old 07-08-08, 06:14 AM
SlaveDriver SlaveDriver is offline
Newbie Coder
 
Join Date: Jan 2008
Posts: 69
Thanks: 0
Thanked 0 Times in 0 Posts
Creating multiple sessions in download script?

Hi folks, not sure if the title really makes any sense but here's my problem :

I have a download script, which checks if a user is logged in before downloading.

the first line of code in my download script is
session_start();

Everythign works correctly, however if a user is downloading a file, the user will be unable to browse the website untill the download has finished.

I came up with a solution (but it's hardly practical) by adding:
session_destroy();

after the login check functions.

This is very impractical as it will end the session and will require the user to relogin when browsing. But it does solve the problem of restricing browsing when downloading

IS there a more practical solution to this problem?

Thank you coders
Reply With Quote
  #2 (permalink)  
Old 07-08-08, 10:30 AM
Dissonance Dissonance is offline
Newbie Coder
 
Join Date: Jul 2008
Location: Kentucky, USA
Posts: 24
Thanks: 0
Thanked 0 Times in 0 Posts
If you are not naming your sessions, I suggest you do so.

PHP Code:

session_name('some_name');
session_start(); 
You can create 1 session per page, though I don't know why you'd want more than one session.

How are you checking if a user is logged in? And if you only check that the user is logged in upon downloading, how is the user unable to browse while logged out? Are you checking there as well?
Reply With Quote
  #3 (permalink)  
Old 07-08-08, 03:06 PM
SlaveDriver SlaveDriver is offline
Newbie Coder
 
Join Date: Jan 2008
Posts: 69
Thanks: 0
Thanked 0 Times in 0 Posts
the download script checks if the user is logged in before executing the download.

I don't know the best way of explaining the procdure, so ill keep it short simple.

the download scipt, dl.php is execited like this : site.com/dl.php?file.zip

once the user clicks the link, the dl.php script checks to see if the users is logged in, if so, then the download is executed. if not, then we get error messages.

the problem is, as there is a session_start() in the dl.php file, the user cannot browse until the download is complete.

I foudn a very crap solution by using session_destroy() right before the download is executed,just after the login checks, thus erasing the session data and logging the user out completley, and is able to browse as an anonymous user.

I'll try naming sessions and see how it goes

Still open to more help !

Thanks!
Reply With Quote
  #4 (permalink)  
Old 07-08-08, 04:46 PM
Dissonance Dissonance is offline
Newbie Coder
 
Join Date: Jul 2008
Location: Kentucky, USA
Posts: 24
Thanks: 0
Thanked 0 Times in 0 Posts
Personal Preference: I usually add my session initialization inside of my includes.php file, which is referenced by every page. Sessions always exist for all users regardless of whether or not they are logged in.

However, when/if they do login, it saves the session and username(or user id) in the database, for future reference. As sessions can be spoofed, I also store IP addresses, just for that extra bit of security. Physical access to the same network and access to the sessionid (which is pretty hard to get unless you're sniffing network packets) and like people always say, once physical security is breached, security is pretty useless, you can only slow someone's progress.

If it were up to me, I'd tell you to always have sessions created and reference them in a database.

Here is my sessions table. I store data in the time column with PHP's time() format.

sql Code:
  1. --
  2. -- Table structure for table `sessions`
  3. --
  4.  
  5. CREATE TABLE IF NOT EXISTS `sessions` (
  6.   `ip` varchar(15) collate latin1_general_ci NOT NULL,
  7.   `time` varchar(15) collate latin1_general_ci NOT NULL,
  8.   `id` varchar(32) collate latin1_general_ci NOT NULL,
  9.   `name` varchar(32) collate latin1_general_ci NOT NULL,
  10.   `userid` int(11) NOT NULL,
  11.   PRIMARY KEY  (`id`)
  12. ) ENGINE=MyISAM DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci;

PHP Code:

function is_user_logged_in()
{
    
$link database_connect(); 
    
$sql "SELECT * FROM sessions WHERE ip='$_SERVER[REMOTE_ADDR]'"// SQL Query
    
$result mysql_query($sql$link); // $link is the database connection.  (Not many people use this, but I do.)
    
$num mysql_num_rows($result); // How many rows?  (Should just be one, but on the off-chance that we have multiple users from the same IP, such as a college or something...)
    
for ($i 0$i $num$i++) // Loop through all the rows
    
{
        
$row mysql_fetch_assoc($result); // Grab a row.
         // If session ID is the same as the stored database value, success!
        
if (session_id() == $row['id']) { return true; } 
    }
    return 
false;


Last edited by Dissonance; 07-08-08 at 04:49 PM.
Reply With Quote
  #5 (permalink)  
Old 07-09-08, 02:50 AM
Nico's Avatar
Nico Nico is offline
Community Leader
 
Join Date: Sep 2005
Location: Spain
Posts: 8,075
Thanks: 11
Thanked 88 Times in 83 Posts
Maybe I'm missing something, but wouldn't it be better to query the database like this:

PHP Code:

$session_id session_id();

$sql "
    SELECT * FROM sessions
    WHERE
        ip = '
{$_SERVER['REMOTE_ADDR']}' AND 
        id = '
{$session_id}'
    LIMIT 1"

...?
Reply With Quote
  #6 (permalink)  
Old 07-09-08, 03:10 AM
Dissonance Dissonance is offline
Newbie Coder
 
Join Date: Jul 2008
Location: Kentucky, USA
Posts: 24
Thanks: 0
Thanked 0 Times in 0 Posts
This is very true. I remember writing the original code that only checked IPs, which didnt work with multiple users from a single IP, so I added in the last few lines to check SessionID's as well, fixing the problem.

I guess I hadn't rewritten that part just yet.

Great catch though, I'll update my scripts accordingly.
Reply With Quote
  #7 (permalink)  
Old 07-12-08, 03:39 PM
itsjazzy itsjazzy is offline
New Member
 
Join Date: Jul 2008
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Quote:
Originally Posted by SlaveDriver View Post
I foudn a very crap solution by using session_destroy() right before the download is executed,just after the login checks, thus erasing the session data and logging the user out completley, and is able to browse as an anonymous user.

I'll try naming sessions and see how it goes

Still open to more help !

Thanks!
Try using session_write_close() instead of session_destroy()


Cheers

Andy
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
looking for script! to download files from other servers on a sever dmc936 Script Requests 2 10-17-07 01:51 AM
2 profitable script sites for sale cms-master.com General Advertisements 3 07-03-07 10:17 AM
Raffle/Lottery Script (Very profitable!), Coded it myself. Voltaire General Advertisements 2 01-02-06 11:55 PM
Free/Commercial PHP Software Script Download System SaN-DeeP Script Requests 0 08-30-05 01:29 AM
error when creating database tables with php script spiroth10 PHP 4 01-06-04 03:59 PM


All times are GMT -5. The time now is 07:49 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.