Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

DATETIME, Form UPDATE problems...PHP

Posted on 2007-07-24
9
Medium Priority
?
219 Views
Last Modified: 2013-12-13
Hello.  I have a register user page set up that allows an administrator to create an account for a user.  However, I am wanting to build onto this code to develop a way to create a 1-day, temporary account.  I feel as if I am getting really close...I just need some quick help...

What I am wanting to do is this...

 - First I need to find a correct way while registering to also INSERT INTO my SQL table the timestamp for field 'Creation_date' type: DATETIME...For some reason, that is the only field that is not working! (****Check my code below****)

 - I have added a checkbox to my form.  Its value is 'checkbox'.  When all information is filled out, the checkbox is clicked, and the form is submitted, I need the script to set the 'Temporary' field equal to '1'...

Here is my php code so far...
register_user.php
=========================
<?php
session_start();
include_once('log.php');

if (!isset($_SESSION['logged_in'] ))
{
    header("Location: access_denied.php");
}

if ($_SESSION['Level_access'] != 1)
{
    header('Location: access_denied.php');
}

require_once('db.php');
include('functions.php');

      if(isset($_POST['register']))
      {
            if($_POST['username']!='' && $_POST['password']!='' && $_POST['password']==$_POST['password_confirmed'] && $_POST['email']!='' && valid_email($_POST['email'])==TRUE && checkUnique('Username', $_POST['username'])==TRUE && checkUnique('Email', $_POST['email'])==TRUE)
            {
            
                  $today = getdate();
                  $query = mysql_query("INSERT INTO users (`Username` , `Password`, `Email`, `Random_key`, `Creation_date`) VALUES ('".mysql_real_escape_string($_POST['username'])."', '".mysql_real_escape_string(md5($_POST['password']))."', '".mysql_real_escape_string($_POST['email'])."', '".random_string('alnum', 32)."', '".$today."' )") or die(mysql_error());
                  
                  $getUser = mysql_query("SELECT ID, Username, Email, Random_key FROM users WHERE Username = '".mysql_real_escape_string($_POST['username'])."'") or die(mysql_error());
      
                  if(mysql_num_rows($getUser)==1)
                  {                  
                        $row = mysql_fetch_assoc($getUser);
                        $headers =       "From: Dialer Secure Server";
                      $headers =      "Reply-To: something@something.com";
                        $subject = "Activation email for Dialer Secure Server";
                        $message = "Dear ".$row['Username'].", this is your activation link to be added to the TFS Dialer Secure Server Database. In order to confirm your user account, please click on the following link: http://*.*.*.*/dialer/confirm.php?ID=".$row['ID']."&key=".$row['Random_key']." \n\n Please remember to change your password upon first login!!!\n\n Thank you for joining!";
                        if(mail($row['Email'], $subject, $message, $headers))
                        {
                              header('Location: registration_complete.php');
                        }
                        else {
                              header('Location: registration_error.php');
                        }
                  }
                  else {
                        header('Location: registration_error.php');
                  }
                                          
            }
            else {            
                  header('Location: registration_error.php');      
            }
      }
?>
<?php if(isset($error)){ echo $error;}?>
<?php if(isset($msg)){ echo $msg;} else {}?>
0
Comment
Question by:dialeradmin
  • 5
  • 4
9 Comments
 
LVL 49

Expert Comment

by:Roonaan
ID: 19557851
Please make sure that $today is in the format yyyy-mm-dd hh:mm:ss.

The call '$today = getdate()' will not provide you with such a format. The getdate() functions returns an array will all different date parts, which you can access through $today['minutes'], $today['seconds'] etc.

There are two things you can do:
1. Instead of using '".$today."' use the mysql now() function, such that your query becomes INSERT INTO table (name, timefield) values('text', now())
2. Use $today = date('y-m-d h:i:s'). This will provide you with the required date format

Hope this helps,

Kind regards

-r-
0
 

Author Comment

by:dialeradmin
ID: 19564922
Sounds good.  Thanks.
...so now then, if the checkbox is clicked, what could be a way to set 'Temporary' equal to '1' when the registration information is completed, the checkbox is marked, and the form is submitted?
0
 
LVL 49

Expert Comment

by:Roonaan
ID: 19565147
use

$temporaryValue = isset($_POST['checkboxname']) ? 1 : 0;

and then add it to your query:

"INSERT INTO users(...... , Temporary) values ( ........ , '".$temporaryValue.'")";

-r-
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:dialeradmin
ID: 19565642
Ahhh...Well, that worked, but I forgot to mention:

 - If the checkbox is marked during registration, it sets the account 'Temporary' equal to '1'.....but I need to also set the field 'Level_access' to '4'...

Here is the code it works with:
===========================
if(isset($_POST['register']))
      {
            if($_POST['username']!='' && $_POST['password']!='' && $_POST['password']==$_POST['password_confirmed'] && $_POST['email']!='' && valid_email($_POST['email'])==TRUE && checkUnique('Username', $_POST['username'])==TRUE && checkUnique('Email', $_POST['email'])==TRUE)
            {
            
                  $today = date('y-m-d h:i:s');
                  $temporaryValue = isset($_POST['checkbox']) ? 1 : 0;
                  $query = mysql_query("INSERT INTO users (`Username` , `Password`, `Email`, `Random_key`, `Temporary`, `Creation_date`) VALUES ('".mysql_real_escape_string($_POST['username'])."', '".mysql_real_escape_string(md5($_POST['password']))."', '".mysql_real_escape_string($_POST['email'])."', '".random_string('alnum', 32)."', '".$temporaryValue."', '".$today."' )") or die(mysql_error());
                  
                  $getUser = mysql_query("SELECT ID, Username, Email, Random_key FROM users WHERE Username = '".mysql_real_escape_string($_POST['username'])."'") or die(mysql_error());
0
 
LVL 49

Accepted Solution

by:
Roonaan earned 2000 total points
ID: 19565677
$temporaryValue = isset($_POST['checkbox']) ? 1 : 0;
$levelValue = isset($_POST['checkbox']) ? 4 : 0;

                  $query = mysql_query("INSERT INTO users (`Username` , `Password`, `Email`, `Random_key`, `Temporary`, `Level_access`, `Creation_date`) VALUES ('".mysql_real_escape_string($_POST['username'])."', '".mysql_real_escape_string(md5($_POST['password']))."', '".mysql_real_escape_string($_POST['email'])."', '".random_string('alnum', 32)."', '".$temporaryValue."', '".$levelValue."','".$today."' )") or die(mysql_error());
                 
                  $getUser = mysql_query("SELECT ID, Username, Email, Random_key FROM users WHERE Username = '".mysql_real_escape_string($_POST['username'])."'") or die(mysql_error());
0
 

Author Comment

by:dialeradmin
ID: 19565776
Awesome.  Thanks for your help!!!

 - Just a side note, I'm needing to create a server side script that actually scans the 'users' SQL database every hour.  When it finds an account where 'Temporary' is set to '1' and its 'Creation_date' is past 24 hours, it deletes that record from the database....What language could I use to create something like this?  Do you have any suggestions?  Thanks again.  
0
 
LVL 49

Expert Comment

by:Roonaan
ID: 19565801
You could use a php script and use cron/cronjob to call your script every hour.

Are you on a shared hosting plan, or on a private server?

-r-
0
 

Author Comment

by:dialeradmin
ID: 19565823
I'm on a private server...
0
 
LVL 49

Expert Comment

by:Roonaan
ID: 19565878
Depending on whether it is windows or *nix, you'd then use cron or just the task manager. For windows you could download and install wget (http://www.gnu.org/software/wget) and call it from your task:
wget -a log.txt -O - http://domain/maintenance/my-maintenance-script.php

A similar syntax (if not just the same) can be used on *nix, although wget is probably available by default and doesn't require a manual installation.

Hope this helps

-r-
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

These days socially coordinated efforts have turned into a critical requirement for enterprises.
This article discusses four methods for overlaying images in a container on a web page
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses

564 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question