Hey all

I have a request i need to help implement

We have this company who has 10 packages they need to distribute for download to 21 departments via the internet

Eg.
User1 logs in, then he clicks on accept, and then goes to a page that shows he can download file1, file3, and file5

User2 logs in and clicks on accept (disclaimer page) then goes to page where he sees he can download file1-file10

I suspect it can be done with a Mysql with coloums for
user, file1, file2, file3..
user1,yes,yes,no
user2,no,yes,yes

and then if quering to generate the page depending on whether the user has a yes for the file or not enabling the Download link

Does anyone know of such a script?

Well they are extremely easy to make. Do you have much PHP/MySql knowledge?

I could probably whack one out in a few hours. Of course for me to give you any ideas of how this might be done I would need to know what you have so far so as we could build around this.

Is this a short term or long term process?
If not you can just program it so the users are sent to the page automatically, else you can program in mysql.
There are various ways you can set up the database if your doing that(your way) I suggest:
user id/user/file1/file 2/etc
1/bob/y/n/
2/smith/n/n/

Then just make a page with links to these files.
Then if statements in between these links(if Y then display link).

Understand my logic? Let me know how you go.

Your logic is exactly what i had in mind
I cannot code, though i can modify a chunk of code...

So say i have the folowing:

DB:
Host: localhost
username_1
passwd_1
DB: dac_project

The 10 coloums + name/id is exactly what i need

I can go as far as setting up such a DB manually
1/User/y/y/y/n/n/n/y/y/n/n
2/user2/n/n/n/n/n/n/n/n/y/y

Then how would i code the PHP page

I expect is have a login, that checks the DB, and parses the username to a variable

Then the links page has a "if user = %user% then look in %user% row on DB and print this: <a href... bla bla) where coloums 1 = yes, else print <not allowed>

Can you perhaps just point me in two directions?

1. Code for DB connection
2. Code for the If query

Then i can see what i manage


Is this a short term or long term process?
If not you can just program it so the users are sent to the page automatically, else you can program in mysql.
There are various ways you can set up the database if your doing that(your way) I suggest:
user id/user/file1/file 2/etc
1/bob/y/n/
2/smith/n/n/

Then just make a page with links to these files.
Then if statements in between these links(if Y then display link).

Understand my logic? Let me know how you go.

hi,
1:connection to database:

<?php mysql_connect("Localhost","Root","Psw");
mysql_select_db("your db name");

2:code for if:

<? $query=mysql_query("select * from `database name` ");
$fet=mysql_fetch_assoc($query);
//before display
if($fet['file1']=='y'){  //show}
else { // donot show}

for user you can use sessions i.e. insert user id in session and check for that user or not

I see your logic but it is somewhat floored.

If you just create a table that will hold your users details WITH AN ID!

Then you can create another table that will hold files. For instance:

file_id | owner_id | name | source | downloads

This is of course if you want to count the downloads done the source will hold the http:// source of the file, the name will be the name of the file and the file_id will be the identinfying primary key. The most important one is the owner_id, this should be the same as the users id.

Then you can run a simple query using the users id:

// The cookie holding the users username
$username = $_COOKIE['username'];
// The id from the table using thr users username to pull the row
$user_id = fetch("SELECT `owner_id` FROM `users` WHERE `username` = '$username'");
// Run the query to select all of the downloads for the specified user
$query = mysql_query("SELECT * FROM `users` WHERE `owner_id` = '$user_id'");
// Run a loops to echo out the downloads
while( $row = mysql_fetch_array( $query ) ) {
     echo '<p>Name: '.$row['name'].'</p>';
     echo '<p>Downloads: '.$row['downloads'].'</p>';
     echo '<p><a href="download.php?dl_id='.$row['id'].'">Download the file here.</a></p>';
}

This is ofcourse only if you wish to count the downloads and try (a little bit) to hide the actual url of the file. The download.php would run similar querys to make sure the user has permition to download this file then return maybe an ads page with a meta refresh tag that will redirect them to the file.

Just some thoughts for you to play on.

I tried playing around with it this weekend, and seeing as my deadline is Friday i'm getting a bit worried i might not be able to do it

Okay, so i went into PHPmyadmin, created a DB called "user"
then under that i created a Table for "test" and created 13 fields
ID, user, pass, file1, file2, file3, file4, file5, file6, file7, file8, file9 and file10

Can you please tell write the code for me (admin page)

to Create User/Edit

with text fields for the user/passwd, then 10 x radio boxes to change the file1-fil10 fields to yes, or no depending on whether the radio box is on?

Then the page for downloads

I tried a lot to use an existing login script and just modify it, but i just dont understand PHp well enough

Can i do it all on one PHp page correct?

This is the script i tried modifying main.php

<?
include("include/session.php");
?>

<html>
<title>bla bla</title>
<body>

<table>
<tr><td>


<?
/**
 * User has already logged in, so display relavent links.
 */
if($session->logged_in){
   echo "<h1>Logged In</h1>";


<<<<<<<<<<<<<then the If querie part here">>>>>>>>>>>>>>>>>>>>>>>>>>



   echo "Welcome <b>$session->username</b>, you are logged in. <br><br>"
   echo "[<a href=\"process.php\">Logout</a>]";
}
else{
?>
<h1>Login</h1>
<?
/**
 * User not logged in, display the login form.
 * If user has already tried to login, but errors were
 * found, display the total number of errors.
 * If errors occurred, they will be displayed.
 */
if($form->num_errors > 0){
   echo "<font size=\"2\" color=\"#ff0000\">".$form->num_errors." error(s) found</font>";
}
?>
<form action="process.php" method="POST">
<table align="left" border="0" cellspacing="0" cellpadding="3">
<tr><td>Username:</td><td><input type="text" name="user" maxlength="30" value="<? echo $form->value("user"); ?>"></td><td><? echo $form->error("user"); ?></td></tr>
<tr><td>Password:</td><td><input type="password" name="pass" maxlength="30" value="<? echo $form->value("pass"); ?>"></td><td><? echo $form->error("pass"); ?></td></tr>
<tr><td colspan="2" align="left"><input type="checkbox" name="remember" <? if($form->value("remember") != ""){ echo "checked"; } ?>>
<font size="2">Remember me next time &nbsp;&nbsp;&nbsp;&nbsp;
<input type="hidden" name="sublogin" value="1">
<input type="submit" value="Login"></td></tr>
<tr><td colspan="2" align="left"><br><font size="2">[<a href="forgotpass.php">Forgot Password?</a>]</font></td><td align="right"></td></tr>
</table>
</form>


</td></tr>
</table>


</body>
</html>

Since this script already parses the Username to a $session->username

I think this part:

// Run the query to select all of the downloads for the specified user

$query = mysql_query("SELECT * FROM `users` WHERE `owner_id` = '$user_id'");

// Run a loops to echo out the downloads

while( $row = mysql_fetch_array( $query ) ) {

echo '<p>Name: '.$row['name'].'</p>';

echo '<p>Downloads: '.$row['downloads'].'</p>';

echo '<p><a href="download.php?dl_id='.$row['id'].'">Download the file here.</a></p>';

}

If i understand it correctly can just be modified to read that session already created? Correct?

Please Help

The script above is http://evolt.org/files/Login_System_v.2.0.zip

I'm having another problem

When i add more fields to the table "users" in the database it bombs out and cannot "register" - gives an error and bombs out

I added fields by doing this:

CREATE TABLE users (
 username varchar(30) primary key,
 password varchar(32),
 userid varchar(32),
 userlevel tinyint(1) unsigned not null,
 email varchar(50),
 timestamp int(11) unsigned not null,
file1 varchar(32),
file2 varchar(32),
file3 varchar(32),
file4 varchar(32),
file5 varchar(32),
file6 varchar(32),
file7 varchar(32),
file8 varchar(32),
file9 varchar(32),
file10 varchar(32)
);

Any ideas. You can download the script yourself, basically the easiest way i think would be to take the $session->username variable in main.php and have that take "if for this username, file1 = yes then echo a href, else echo not allowed", and if file2 = yes then echo href else echo not allowed

Can you please take a look and modify main.php for me?

The admin section can also be easily made using regsiter.php and just add fields for the 10 files using the checkbox thing i asked in previous post?

I may be able to do it for you...

It'd be easier if you have the users id or username in the cookie?

You will need to add another table!

Execute this for the table

CREATE TABLE IF NOT EXISTS `files` (
  `file_id` int(2) NOT NULL,
  `file_name` varchar(200) NOT NULL,
  `file_src` varchar(200) NOT NULL,
  PRIMARY KEY (`file_id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;

And this for the values. Change the 'File (?) name' for the name of the file and change the 'File (?) HTTP source' for the address of the file eg. http://mydomain.com/filename.zip.
** (?) being the file number!!! **

INSERT INTO `files` (`file_id`, `file_name`, `file_src`) VALUES
(2, 'File 2 name', 'File 2 HTTP source'),
(1, 'File 1 name', 'File 1 HTTP source'),
(3, 'File 3 name', 'File 3 HTTP source'),
(4, 'File 4 name', 'File 4 HTTP source'),
(5, 'File 5 name', 'File 5 HTTP source'),
(6, 'File 6 name', 'File 6 HTTP source'),
(7, 'File 7 name', 'File 7 HTTP source'),
(8, 'File 8 name', 'File 8 HTTP source'),
(9, 'File 9 name', 'File 9 HTTP source'),
(10, 'File 10 name', 'File 10 HTTP source');

Then use this code please note that the commented queries are dependant on wether you store the user id in the cookie or the username in the cookie. If you use neither then you, if you don't mind me saying, fucked. You will have to go back and change the login to add the users username or id to the cookie value!

<?
include("include/session.php");
?>

<html>
<title>bla bla</title>
<body>

<table>
<tr><td>


<?
/**
* User has already logged in, so display relavent links.
*/
if($session->logged_in){
echo "<h1>Logged In</h1>";


<h2>Your available downloads</h2>
//$id = $_COOKIE['logged_in'];
//$query = mysql_query("SELECT * FROM `users` WHERE `id` = '$id'");

$user = $_COOKIE['logged_in'];
$query = mysql_query("SELECT * FROM `users` WHERE `username` = '$user'");

// This will echo out all the links to the downloads
$count = 1;
while( $row = mysql_fetch_array( ) ) {
    if( $row[file$count] == 1 ) {
         $file_dets = fetch("SELECT * FROM `files` WHERE `file_id` = '$count'");
         echo '<a href="'.$file_dets['file_src'].'" target="_blank">'.$file_dets['file_name'].'</a><br />';
    }
    $count++;
}
echo '<br/ >';
echo "Welcome <b>$session->username</b>, you are logged in. <br><br>"
echo "[<a href=\"process.php\">Logout</a>]";
}
else{
?>
<h1>Login</h1>
<?
/**
* User not logged in, display the login form.
* If user has already tried to login, but errors were
* found, display the total number of errors.
* If errors occurred, they will be displayed.
*/
if($form->num_errors > 0){
echo "<font size=\"2\" color=\"#ff0000\">".$form->num_errors." error(s) found</font>";
}
?>
<form action="process.php" method="POST">
<table align="left" border="0" cellspacing="0" cellpadding="3">
<tr><td>Username:</td><td><input type="text" name="user" maxlength="30" value="<? echo $form->value("user"); ?>"></td><td><? echo $form->error("user"); ?></td></tr>
<tr><td>Password:</td><td><input type="password" name="pass" maxlength="30" value="<? echo $form->value("pass"); ?>"></td><td><? echo $form->error("pass"); ?></td></tr>
<tr><td colspan="2" align="left"><input type="checkbox" name="remember" <? if($form->value("remember") != ""){ echo "checked"; } ?>>
<font size="2">Remember me next time &nbsp;&nbsp;&nbsp;&nbsp;
<input type="hidden" name="sublogin" value="1">
<input type="submit" value="Login"></td></tr>
<tr><td colspan="2" align="left"><br><font size="2">[<a href="forgotpass.php">Forgot Password?</a>]</font></td><td align="right"></td></tr>
</table>
</form>


</td></tr>
</table>


</body>
</html>

[b]

Since this script already parses the Username to a $session->username

I think this part:

#
// Run the query to select all of the downloads for the specified user
#
$query = mysql_query("SELECT * FROM `users` WHERE `owner_id` = '$user_id'");
#
// Run a loops to echo out the downloads
#
while( $row = mysql_fetch_array( $query ) ) {
#
echo '<p>Name: '.$row['name'].'</p>';
#
echo '<p>Downloads: '.$row['downloads'].'</p>';
#
echo '<p><a href="download.php?dl_id='.$row['id'].'">Download the file here.</a></p>';
#
}
#

This should work, try it and see if it does, if it doesn't then send me the error or tell me whats not working and I will try and get this done by your deadline.

I did this in the daniweb reply box so I don't know it this will indeed work without a hitch.

Parse error: syntax error, unexpected '<' in /home/bluetek/public_html/dac/main.php on line 20

Okay found the problem myself on that one

in

<h2>Your available downloads</h2>

i changed it to

echo "<h2>Your available downloads</h2>"

Now i get

Parse error: syntax error, unexpected T_VARIABLE, expecting ',' or ';' in /home/bluetek/public_html/dac/main.php on line 24

Okay found the problem myself on that one

in

<h2>Your available downloads</h2>

i changed it to

echo "<h2>Your available downloads</h2>"

Now i get

Parse error: syntax error, unexpected T_VARIABLE, expecting ',' or ';' in /home/bluetek/public_html/dac/main.php on line 24
echo "<h2>Your available downloads</h2>"

you missed the closing ;

echo "<h2>Your available downloads</h2>";

Parse error: syntax error, unexpected T_VARIABLE, expecting ']' in /home/bluetek/public_html/dac/main.php on line 30

Line 30 is this part:

// This will echo out all the links to the downloads
28  
$count = 1;
29
while( $row = mysql_fetch_array( ) ) {
30
if( $row[file$count] == 1 ) }
31
$file_dets = fetch("SELECT * FROM `files` WHERE `file_id` = '$count'");
echo '<a href="'.$file_dets['file_src'].'" target="_blank">'.$file_dets['file_name'].'</a><br />';
}
$count++;
}
echo '<br/ >';
echo "Welcome <b>$session->username</b>, you are logged in. <br><br>"
echo "[<a href=\"process.php\">Logout</a>]";

thus this line

if( $row[file$count] == 1 ) }

Parse error: syntax error, unexpected T_VARIABLE, expecting ']' in /home/bluetek/public_html/dac/main.php on line 30

Line 30 is this part:

// This will echo out all the links to the downloads
28  
$count = 1;
29
while( $row = mysql_fetch_array( ) ) {
30
if( $row[file$count] == 1 ) }
31
$file_dets = fetch("SELECT * FROM `files` WHERE `file_id` = '$count'");
echo '<a href="'.$file_dets['file_src'].'" target="_blank">'.$file_dets['file_name'].'</a><br />';
}
$count++;
}
echo '<br/ >';
echo "Welcome <b>$session->username</b>, you are logged in. <br><br>"
echo "[<a href=\"process.php\">Logout</a>]";

thus this line

if( $row[file$count] == 1 ) }

... ... ... then fix it, you can see where the error is . file$count is not a valid variable

Josh, please help!

I have no idea?

Anyone else?

i changed it to

if( $row[file_count] == 1 ) {

Now i get

Parse error: syntax error, unexpected $end in /home/bluetek/public_html/dac/main.php on line 104

Line 104 is:

</td></tr>

</table>


</body>
</html>


Just to recap - here is the entire file

<?php
include("include/session.php");
?>
<html>
<title>bla bla</title>
<body>
 
<table>
<tr><td>
 
 
<?php
/**
* User has already logged in, so display relavent links.
*/
if($session->logged_in){
echo "<h1>Logged In</h1>";
 
 
echo "<h2>Your available downloads</h2>";
//$id = $_COOKIE['logged_in'];
//$query = mysql_query("SELECT * FROM `users` WHERE `id` = '$id'");
 
$user = $_COOKIE['logged_in'];
$query = mysql_query("SELECT * FROM `users` WHERE `username` = '$user'");
 
// This will echo out all the links to the downloads
$count = 1;
while( $row = mysql_fetch_array( ) ) {
if( $row[file_count] == 1 ) {
$file_dets = fetch("SELECT * FROM `files` WHERE `file_id` = '$count'");
echo '<a href="'.$file_dets['file_src'].'" target="_blank">'.$file_dets['file_name'].'</a><br />';
}
$count++;
}
echo '<br/ >';
echo "Welcome <b>$session->username</b>, you are logged in. <br><br>";
echo "[<a href=\"process.php\">Logout</a>]";
}
else{

?>

<h1>Login</h1>

<?php

/**

* User not logged in, display the login form.

* If user has already tried to login, but errors were

* found, display the total number of errors.

* If errors occurred, they will be displayed.

*/

if($form->num_errors > 0){

echo "<font size=\"2\" color=\"#ff0000\">".$form->num_errors." error(s) found</font>";

}

?>

<form action="process.php" method="POST">

<table align="left" border="0" cellspacing="0" cellpadding="3">

<tr><td>Username:</td><td><input type="text" name="user" maxlength="30" value="<?php echo $form->value("user"); ?>"></td><td><?php echo $form->error("user"); ?></td></tr>

<tr><td>Password:</td><td><input type="password" name="pass" maxlength="30" value="<?php echo $form->value("pass"); ?>"></td><td><?php echo $form->error("pass"); ?></td></tr>

<tr><td colspan="2" align="left"><input type="checkbox" name="remember" <?php if($form->value("remember") != ""){ echo "checked"; } ?>>

<font size="2">Remember me next time &nbsp;&nbsp;&nbsp;&nbsp;

<input type="hidden" name="sublogin" value="1">

<input type="submit" value="Login"></td></tr>

<tr><td colspan="2" align="left"><br><font size="2">[<a href="forgotpass.php">Forgot Password?</a>]</font></td><td align="right"></td></tr>

</table>

</form>

 

 

</td></tr>

</table>


</body>
</html>
<?php
include("include/session.php");
?>
<html>
<title>bla bla</title>
<body>
 
<table>
<tr><td>
 
 
<?php
/**
* User has already logged in, so display relavent links.
*/
if($session->logged_in){
echo "<h1>Logged In</h1>";
 
 
echo "<h2>Your available downloads</h2>";
//$id = $_COOKIE['logged_in'];
//$query = mysql_query("SELECT * FROM `users` WHERE `id` = '$id'");
 
$user = $_COOKIE['logged_in'];
$query = mysql_query("SELECT * FROM `users` WHERE `username` = '$user'");
 
// This will echo out all the links to the downloads
$count = 1;
while( $row = mysql_fetch_array( $query ) ) {
$file_count = "file".$count;
if( $row[$file_count] == 1 ) {
$file_dets = fetch("SELECT * FROM `files` WHERE `file_id` = '$count'");
echo '<a href="'.$file_dets['file_src'].'" target="_blank">'.$file_dets['file_name'].'</a><br />';
}
$count++;
}
echo '<br/ >';
echo "Welcome <b>$session->username</b>, you are logged in. <br><br>";
echo "[<a href=\"process.php\">Logout</a>]";
}
else{

?>

<h1>Login</h1>

<?php

/**

* User not logged in, display the login form.

* If user has already tried to login, but errors were

* found, display the total number of errors.

* If errors occurred, they will be displayed.

*/

if($form->num_errors > 0){

echo "<font size=\"2\" color=\"#ff0000\">".$form->num_errors." error(s) found</font>";

}

?>

<form action="process.php" method="POST">

<table align="left" border="0" cellspacing="0" cellpadding="3">

<tr><td>Username:</td><td><input type="text" name="user" maxlength="30" value="<?php echo $form->value("user"); ?>"></td><td><?php echo $form->error("user"); ?></td></tr>

<tr><td>Password:</td><td><input type="password" name="pass" maxlength="30" value="<?php echo $form->value("pass"); ?>"></td><td><?php echo $form->error("pass"); ?></td></tr>

<tr><td colspan="2" align="left"><input type="checkbox" name="remember" <?php if($form->value("remember") != ""){ echo "checked"; } ?>>

<font size="2">Remember me next time &nbsp;&nbsp;&nbsp;&nbsp;

<input type="hidden" name="sublogin" value="1">

<input type="submit" value="Login"></td></tr>

<tr><td colspan="2" align="left"><br><font size="2">[<a href="forgotpass.php">Forgot Password?</a>]</font></td><td align="right"></td></tr>

</table>

</form>

 

 

</td></tr>

</table>


</body>
</html>

Should work now.

Just to clarify, does the cookie holsd the username? if not then the script needs to be changed...

Infact are you using PHP Sessions?

The sooner you tell me these things the sooner I can send you on your way.

I doint actually know thats why i'm posting all the code for you as well as the link to the download

I'm starting to think, maybe i should ask you to whack it out entirely for me, you seem to know what you are doing, so modifying another script has lots of pitfalls

Can i get you Email adress, then i can send you all the mysql, ftp details etc as well, so you can test immediately without having to wait for my reply?

Just to clarify, does the cookie holsd the username? if not then the script needs to be changed...

Infact are you using PHP Sessions?

The sooner you tell me these things the sooner I can send you on your way.

I suppose so, I don't feel to happy taking FTP details as I only take those kind of details for a project or paid services. This way the user (i.e you) has some kind of security.

I think I might be able to email me at: josh [at] jjcon.co.uk

Can I ask, who exactly originally programmed these scripts? It's a little wierd as there is a mish mash of different PHP versions...

Usually a PHP programmer programs in just one version (usually the one they learned with) I myself am prodominently a PHP 4 & 5 programmer but I can program in PHP 6 aswell.

I dont know. I just downloaded it.

I have found a perfect sollution though:

I spent the last 5 hours setting it up, but it works, out of the box:

Joomla1.5 CMS:
Installed DocMan

Added the ten files into it
Then created ten groups (file1-file10) and assigned the files to that security group

eg file1 - group1, file2 - group2 etc

Then in Joomla User Manager i added the 21 users

Back in Docman Group config, i just added the users manually to the groups they have rights to

eg. user1 is member of group1,3,4,5 and user2 is member of groups 1,3,6,8,9,10

And now it work!

You have to love open source. Wouldve loved to be able to do it in code, but i guess i'm still a year or so from being able to do it. Besides, open source Turnkey solutions works 75% of the time.

Sorry for wasting all your time

If you do get a standalone script done, contact me and i might replace the Joomla one - since the project might grow to 1000+ users over the next 12 months and manually adding them all is a bit of a schlep

It's better off you contact us when you need something like that, besides you usually hire a freelancer to do something like that.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.