Jump to content
Larry Ullman's Book Forums
Sign in to follow this  
adriandaniel

Chapter 13 - Activate.php Page

Recommended Posts

Hi Guys,

 

I have been using Larry's book recently and I must say it is amazing how straight forward it is and how much is helping me learn  PHP and MYSQL.

My question is from chapter 13. I managed to make everything  work beside the activate.php page which I send to the user via email so they can activate their account. When they click  on the link from the  email the browser doesn't find the page although it is in the same folder on the server as the other files. What can be the cause of that?

 

Kind Regards,

Adrian

Share this post


Link to post
Share on other sites

We have very little to go on here, Adrian. No code, no link (even relative for your site) or other info needed to help you out. It will be purely guesswork.

 

To actually guess, I would double check your assumptions are true. Is the file there? Is it Chmodded like the rest of your files (You can often check this with FTP - try right clicking the file and look for CHMOD/Permissions) and does the link actually point to the directory the file resides in?

 

If non of that does help, please provide some more info. ;)

Share this post


Link to post
Share on other sites

Hi Antonio,

 

My apologies,  I didn't think of putting code. I believe the problem is that in the link sent by email it looks like this http://uel-kbs.co.uk/activate.php12&y=3c6e0ec639a3d4b9127ca4e68fbafe50

 

So the browser is actually looking for the file activate.php including rest of variables. Which is why it  cannot find it on the server as the file is just activate.php 

I know the rest of the variables in that file name represent the user id and status active ( x, y) but how would it know that it actually has to go to the activate.php page? 

Here is the part of the code from the activate.php page  that represents defining the  URL. Maybe this is the problem.

 

// If $x and $y aren't correct, redirect the user.
if ( ($x > 0) && (strlen($y) == 32)) {
 
require_once ('./mysql_connect.php'); // Connect to the database.
$query = "UPDATE users SET active=NULL WHERE (user_id=$x AND active='" . escape_data($y) . "') LIMIT 1";
$result = mysql_query ($query) or trigger_error("Query: $query\n<br />MySQL Error: " . mysql_error());
 
// Print a customized message.
if (mysql_affected_rows() == 1) {
echo "<h3>Your account is now active. You may now log in.</h3>";
} else {
echo '<p><font color="red" size="+1">Your account could not be activated. Please re-check the link or contact the system administrator.</font></p>'; 
}
 
mysql_close();
 
} else { // Redirect.
 
// Start defining the URL.
$url = 'http://' . $_SERVER['HTTP_HOST'] . dirname($_SERVER['PHP_SELF']);
// Check for a trailing slash.
if ((substr($url, -1) == '/') OR (substr($url, -1) == '\\') ) {
$url = substr ($url, 0, -1); // Chop off the slash.
}
// Add the page.
$url .= '/index.php';
 
ob_end_clean(); // Delete the buffer.
header("Location: $url");
exit(); // Quit the script.
 
 
 
And the following thread of coding is from the register page, which generates the email:
 
// Make sure the email address is available.
$query = "SELECT user_id FROM users WHERE email='$e'";
$result = mysql_query ($query) or trigger_error("Query: $query\n<br />MySQL Error: " . mysql_error());
 
if (mysql_num_rows($result) == 0) { // Available.
 
// Create the activation code.
$a = md5(uniqid(rand(), true));
 
// Add the user.
$query = "INSERT INTO users (email, pass, first_name, last_name, active, registration_date) VALUES ('$e', SHA('$p'), '$fn', '$ln', '$a', NOW() )";
$result = mysql_query ($query) or trigger_error("Query: $query\n<br />MySQL Error: " . mysql_error());
 
if (mysql_affected_rows() == 1) { // If it ran OK.
 
// Send the email.
$body = "Thank you for registering at the User Registration site. To activate your account, please click on this link:\n\n";
$body .= "http://uel-kbs.co.uk/activate.php" . mysql_insert_id() . "&y=$a";
mail($_POST['email'], 'Registration Confirmation', $body, 'From: admin@uel-kbs.co.uk');
 
// Finish the page.
echo '<h3>Thank you for registering! A confirmation email has been sent to your address. Please click on the link in that email in order to activate your account.</h3>';
include ('./includes/footer.html'); // Include the HTML footer.
exit();

 

 

 

Hope this  clarifies a bit the problem. 

 

Many thanks,

Adrian

Share this post


Link to post
Share on other sites

Take another look at your link. You've probably looked at it for too long to notice the obvious error. You are not looking for activate.php, but for activate.php12. (Where 12 would be the last inserted id.) My guess is that the link should be .php?x=12&y=MD5().

 

A good general tip is to use very simple debugging steps for problems like this. Don't assume $x or $y holds the correct value, print them to screen along the way. Apply a print_r() to $_GET to make sure you get the correct data, and stick some simple echo statements inside your IF/ELSE-statements to follow the flow of logic. The simple ways are often the best in the end.

 

Good luck. Also, please consider sharing the answer if you find it. It might benefit the next guy with a similar problem. :)

  • Upvote 2

Share this post


Link to post
Share on other sites

Hi Antonio,

 

 

Thank you for taking your time to help me. I really appreciate it. I finally found the issue.  Which is a bit stupid. It was just the activation link  in the registration.php page it was missing the 'x=' before the actual get user id query

 

 

All the best

  • Upvote 1

Share this post


Link to post
Share on other sites

Everyone goes "code blind" after a while looking at the same code. These sort of problems are so much easier to solve with a fresh pair of eyes. The debugging steps I gave you are a safe guard against that.

 

Glad you worked it out. :)

  • Upvote 1

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

×
×
  • Create New...