View Full Version : Login script help

03-07-2007, 04:34 PM
I am making a pay-for-use site that I am having trouble with the login. I have made a few sites before with login pages using Dreamweaver, but this one requires a bit of PHP code. Apparently that is a weak point since I have only read a few books on PHP.

The site will have access for 6 or 12 months. I have a MySQL DB that captures the name, address, etc. It also captures two DATE fields. One for the registration date, reg_date. The other DATE field is for expiration date, expiry_date. For example, if a user registers for 12 months on March 1, the reg_date field is populated with 2007-03-01 and the expiry_date is filled with 2008-03-01.

I would like the login to check these two date fields to grant access to certain pages. I think a little math should do the trick, but am not sure how to do it. BTW- if there is a better way, I am willing to scrap this method and try something else.

I have a small test page produced in DW that I can attach the code here if needed. It uses the DW behaviors to allow access based on a privilege level. I am really a PHP noob and will appreciate any help that is given.

03-07-2007, 07:13 PM
If you are using MySQL 4.1.1 or higher simply pass the DATEDIFF() in a sql statement. If you are using an older version you can use the DATE_SUB().

//This will get all the logins that are OLDER than a year.
$sql = "SELECT * FROM table_name WHERE DATEDIFF(now(), reg_date) >= 365"; // > MySQL 4.1.0

//This will get all the logins that are OLDER than a year.
$sql = "SELECT *, DATE_FORMAT(reg_date,'%m-%d-%Y') AS theDate FROM table_name WHERE DATE_SUB(CURDATE(),INTERVAL 1 YEAR) >= reg_date;"; // < MySQL 4.1.0 syntax
$q = mysql_query($sql);

$i = 0;
while ($r = mysql_fetch_assoc($q)) {
echo "Record Number" . $i;
echo "Registration Date: ";
echo $r[reg_date];
echo " - ";
echo $r[theDate]; //formats the date: 3-07-2007
echo "<br />";

You can use MONTH as an INTERVAL in the second version: INTERVAL 6 MONTH

03-07-2007, 08:22 PM
I checked my hosting company, which is 1and1.
phpinfo shows MySQL version 5.0.18
but SELECT_VERSION shows that they are actually using 4.0.27
so, I guess I should use the second one listed, correct?

03-07-2007, 10:23 PM
Yeah, it should be the one that MySQL reported.

03-08-2007, 05:21 AM
Its funny you say that...I just built a site using 1and1 servers using similar code.....if you are using their servers on perfora.net...then they are 4.0.27 which is soooooooooo out dated...Let me know if you need help. I think you MIGHT need to set the connection and use MYSQL_CLIENT_IGNORE_SPACE so that you can use MYSQL functions..I'm not sure...just use that second query I supplied you with and you'll be okay...