I have a php file that connects to the Invision Power Board database and gets the member data. I need this to restrict downloads to certain members. So when they have a certain ID, they will be able to download the file. Otherwise, they will need to purchase the required membership. I will make it available to guests so you can see what is going on. It is just a picture for testing anyway :p
Everything works fine, until I need to do header() commands in order to download the file. I get a bunch of headers are already set errors: Warning: Cannot modify header information - headers already sent by (output started at /home/program/public_html/CheckLogin.php:35).
<?php
/**
* Edit this path, to where you have your forum installed.
*/
$forum_path = 'forums';
$file = getcwd();
$file = substr($file,0,14);
$file = $file . "downloads/IM2.png";
/**
* We will change directories so that proper directory is picked up
*/
chdir( $forum_path );
/**
* Get some basic IPB files
*/
define( 'IPB_THIS_SCRIPT', 'public' );
require_once( 'initdata.php' );
/**
* Get IPB registry
*/
require_once( IPS_ROOT_PATH . 'sources/base/ipsRegistry.php' );
require_once( IPS_ROOT_PATH . 'sources/base/ipsController.php' );
/**
* initialise the ipsRegistry
*/
$ipbRegistry = ipsRegistry::instance();
$ipbRegistry->init();
$memberData = $ipbRegistry->member()->fetchMemberData();
echo $memberData['member_group_id']. "<br />";
switch ($memberData['member_group_id']){
case 0:
echo "Not sure";
break;
case 1:
echo "Validating";
break;
case 2:
echo "Guests";
break;
case 3:
echo "Member";
break;
case 4:
echo "Root Admin";
break;
case 5:
echo "Not sure";
break;
case 6:
echo "Administrator";
break;
case 7:
echo "Global Moderators";
break;
case 8:
echo "Advanced Member";
break;
case 9:
echo "Not sure";
break;
case 10:
echo "Not sure";
break;
case 11:
echo "Premium Member";
break;
case 12:
echo "Phoenix Member";
break;
case 13:
echo "Not sure";
break;
case 14:
echo "Not sure";
break;
case 15:
echo "Not sure";
break;
case 16:
echo "Not sure";
break;
case 17:
echo "Not sure";
break;
case 19:
echo "Not sure";
break;
}
if($memberData['member_group_id'] == 2)
{
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.basename($file));
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: ' . filesize($file));
ob_clean();
flush();
readfile($file);
exit;
}
else
{
echo "<br /> Not valid";
}
?>
Line 35: $memberData = $ipbRegistry->member()->fetchMemberData();
I NEED this line to connect to the database. The header lines are from php.net, so it should work fine.
Question
+xWhiplash Subscriber²
Hey everyone,
I have a php file that connects to the Invision Power Board database and gets the member data. I need this to restrict downloads to certain members. So when they have a certain ID, they will be able to download the file. Otherwise, they will need to purchase the required membership. I will make it available to guests so you can see what is going on. It is just a picture for testing anyway :p
Everything works fine, until I need to do header() commands in order to download the file. I get a bunch of headers are already set errors: Warning: Cannot modify header information - headers already sent by (output started at /home/program/public_html/CheckLogin.php:35).
<?php /** * Edit this path, to where you have your forum installed. */ $forum_path = 'forums'; $file = getcwd(); $file = substr($file,0,14); $file = $file . "downloads/IM2.png"; /** * We will change directories so that proper directory is picked up */ chdir( $forum_path ); /** * Get some basic IPB files */ define( 'IPB_THIS_SCRIPT', 'public' ); require_once( 'initdata.php' ); /** * Get IPB registry */ require_once( IPS_ROOT_PATH . 'sources/base/ipsRegistry.php' ); require_once( IPS_ROOT_PATH . 'sources/base/ipsController.php' ); /** * initialise the ipsRegistry */ $ipbRegistry = ipsRegistry::instance(); $ipbRegistry->init(); $memberData = $ipbRegistry->member()->fetchMemberData(); echo $memberData['member_group_id']. "<br />"; switch ($memberData['member_group_id']){ case 0: echo "Not sure"; break; case 1: echo "Validating"; break; case 2: echo "Guests"; break; case 3: echo "Member"; break; case 4: echo "Root Admin"; break; case 5: echo "Not sure"; break; case 6: echo "Administrator"; break; case 7: echo "Global Moderators"; break; case 8: echo "Advanced Member"; break; case 9: echo "Not sure"; break; case 10: echo "Not sure"; break; case 11: echo "Premium Member"; break; case 12: echo "Phoenix Member"; break; case 13: echo "Not sure"; break; case 14: echo "Not sure"; break; case 15: echo "Not sure"; break; case 16: echo "Not sure"; break; case 17: echo "Not sure"; break; case 19: echo "Not sure"; break; } if($memberData['member_group_id'] == 2) { header('Content-Description: File Transfer'); header('Content-Type: application/octet-stream'); header('Content-Disposition: attachment; filename='.basename($file)); 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: ' . filesize($file)); ob_clean(); flush(); readfile($file); exit; } else { echo "<br /> Not valid"; } ?>Line 35: $memberData = $ipbRegistry->member()->fetchMemberData();
I NEED this line to connect to the database. The header lines are from php.net, so it should work fine.
Live page - http://www.phstudios.com/CheckLogin.php
Link to comment
Share on other sites
8 answers to this question
Recommended Posts