Solved

Trying to check if url is set, record in database exists, if the time in database hasn't expired and update table, all at once!

Posted on 2016-09-25
7
57 Views
Last Modified: 2016-10-03
I am trying to do a lot on one page and it's getting pretty confusing.  This code is meant to check if 2 GET values are set. If they are, the script has to check both those values against the database. If there is a match it needs to check that the current date has not exceeded 24 hours past the date in the database and if not, it must then update the user records. If there is no match then the user must be redirected or if the time has expired that they have to register has expired they must also be redirected. At the moment, I am getting an error with my new Carbon date and time functionality that I have just found :)

if(!isset($_GET['activecode']) || (!isset($_GET['email']))) {
	
	header("location:page-register.php");
	
	} 

	else {
	
	
	$stmt = $link->prepare("SELECT `user_hash`, `user_email`, `register_date` FROM `db_users` WHERE `user_hash` = ? AND `user_email` = ?");
	$stmt->bind_param("ss", $activate_url, $activate_email);
	$activate_url = urlencode($_GET['activecode']);
	$activate_email = $_GET['email'];
	$stmt->execute();
	$result = $stmt->get_result();
	$numRows = $result->num_rows;
	if($numRows === 1) {
	while($row = $result->fetch_assoc()) {
		
		$db_date = $row['register_date'];

		}
	}
		
		$carbon = new Carbon($db_date);
		$expiry_date = $carbon->copy()->addDays(1);
		$now = Carbon::now();
		
		
		if($now > $expiry_date) {
			
			header("location:expired.php");
			
			} 
	
		
	
		
		else {
		
		
	$stmt = $link->prepare("UPDATE `db_users` SET `user_active` = ?, `user_hash` = ?  WHERE `user_hash` = '$activate_url' AND `user_email` = '$activate_email' LIMIT 1");
	$stmt->bind_param("is", $user_active, $user_hash_reset);	
	$user_active =1;
	$user_hash_reset = 0;
	$stmt->execute();
	$stmt->close();

		
	set_message("<p>Your account has been activated.</p>");
		
		} 
	}

Open in new window


The error I am getting is:

Fatal error: Uncaught Error: Class 'Carbon' not found in.................Stack trace: #0 {main} thrown... line 35

Line 35 is:

$carbon = new Carbon($db_date);

Open in new window

0
Comment
Question by:Black Sulfur
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
7 Comments
 
LVL 1

Author Comment

by:Black Sulfur
ID: 41814970
Wait, never mind about the carbon. I forgot this in the top of my page:

use Carbon\Carbon;

Open in new window


If I put in the correct email and activation code into the url it takes me to the page that says that my activation code has expired which can't be since it is set to 24 hours after I registered. I only registered this morning so the database record should be updating which it's not?
0
 
LVL 35

Assisted Solution

by:Terry Woods
Terry Woods earned 500 total points
ID: 41815485
Try outputting your expiry date (and now, to compare) to understand what's going on:
echo "Expiry date:".$expiry_date->format('Y-m-d H:i:s T');
$now = Carbon::now();
echo "Now:".$now->format('Y-m-d H:i:s T');

Open in new window

0
 
LVL 1

Accepted Solution

by:
Black Sulfur earned 0 total points
ID: 41816703
Okay, so I had a long hard think about this and finally got it to work:

if(isset($_GET['email']) && isset($_GET['activecode'])) {
	
$getEmail = $_GET['email'];
$getCode = $_GET['activecode'];
	
$stmt = $link->prepare("SELECT `user_email`, `user_hash`, `register_date` FROM `db_users` WHERE `user_email` = ? AND `user_hash` = ?");
$stmt->bind_param("ss", $getEmail, $getCode);
$stmt->execute();
$result = $stmt->get_result();
$numRows = $result->num_rows;
if($numRows === 1) {
	
	while($row = $result->fetch_assoc()) {
		
		
		$db_date = new Carbon($row['register_date']); 
		$now = Carbon::now();
		$expiry_date = $db_date->copy()->addDays(1);
		
		if($now > $expiry_date) {
			
			header("location:expired.php");
			
		} else {
			
			// Match found. Update user record.
			$stmt = $link->prepare("UPDATE `db_users` SET `user_active` = ?, `user_hash` = ? WHERE `user_email` = '$getEmail' AND `user_hash` = '$getCode' LIMIT 1");
			$stmt->bind_param("is", $user_active, $user_hash);
			$user_active = 1;
			$user_hash = 0;
			$stmt->execute();
			$stmt->close();
			
			set_message("<div class='contact-success'>Your account has been activated. Please login </div>");
		}
		
		
		} 
	
	} else {
	
	header("location:expired.php");
}
		
} else {
	
	header("location:page-register.php");
}

Open in new window

0
Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

 
LVL 35

Expert Comment

by:Terry Woods
ID: 41816730
Great! If my comments helped, you could accept it as an assisted solution.
0
 
LVL 1

Author Closing Comment

by:Black Sulfur
ID: 41824648
I managed to solve it on my own
0
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 41826520
Just curious... If you're using Carbon, does that imply that you're using Laravel?  If so, the active-record design makes it mostly unnecessary to write your own queries.  If you're not using Laravel, it's not clear to me how Carbon adds any value to the application, over the built-in PHP date handling algorithms.
0
 
LVL 1

Author Comment

by:Black Sulfur
ID: 41826777
Hi ray,

No, not using Laravel or any other framework. I did a tutorial on PHP date and time and they used Carbon which seemed pretty simple to use compared to explanations of php date and time I had seen. But I hadn't come across your article yet so thanks for sharing that. I will certainly take a look at it.

Also, I will admit that I am not great at learning through reading, I normally need to do stuff myself in order to learn anything or watch someone do it (monkey see, monkey do :P)  I absorb more through watching video tutorials (The Carbon one was a video).
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
This article discusses how to create an extensible mechanism for linked drop downs.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

730 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