0

Been trying this for ages :(

Upon registering, each user gets a randomly generated number which is stored in a table called "hash".

After much turmoil I have set up phpmailer and got the whole email process working, all fine, no worries.

BUT I have encountered a problem and I have even read answers on this site that haven't helped. I simply want to create a unique activation link depending on the hash. So for example:

User with "hash" as "12345" gets their verification email, I want the hyperlink to look like this - www.example.com/verify.php?hash=12345

And the same for each other user, here's how I'm currently trying it

<?php
$link_address = "verify.php?hash='.$hash.'";
echo "<a href='$link_address'> Activate </a>";   
?>

This displays "Activate "; ?>"

Very weird :(

EDIT

This is the guide I've previously tried to copy from - http://code.tutsplus.com/tutorials/how-to-implement-email-verification-for-new-members--net-3824

It is outdated and the default mail() function doesn't work for what I want, I'm particularly up to Step 5

EDIT 2

Mail body generation code

$body             = file_get_contents('C:\wamp\www\Game\examples\contents.html');

BIG EDIT!!

New code:

<?php


include("connect.php");

$username = $_POST['username'];


$id_get = mysqli_query($con,"SELECT units.id, users.id, users.username FROM units, users WHERE     users.username = '$username' AND units.id = users.id");
$gotid = mysqli_fetch_array($id_get);
$id4 = $gotid["id"];

$stuff_get = mysqli_query($con,"SELECT users.id, users.email, users.hash, users.username FROM users     WHERE users.username = '$username' AND users.id=$id4");
$gotstuff = mysqli_fetch_array($stuff_get);
$hash2 = $gotstuff['hash'];
$email2 = $gotstuff['email'];
$username2 = $gotstuff['username'];
echo "Your username is ",$username2,"<br>";
echo "Your activation code is ",$hash2,"<br>";

?>

From there I'm trying to get the submitted username (from the registration) and get information from the database (IDs, emails etc) then simply display the username and the hash in the email.

Usually this would be super easy but I'm getting the good old "Notice: Undefined index: username"!!

EDIT

For a completely simple run down on the situation.

I want to be able to get one variable from a page to another page for example.

page1.php

Has a variable called "$users"

page2.php

Wants to echo $users

But I do not know how to make the variable $users specifically from page1.php available on page2.php!

Ryan Mckenna
  • 53
  • 1
  • 7
  • It looks like you might be sending PHP source code in an email. You need to execute the PHP and get the HTML output to send. – Quentin Oct 13 '14 at 16:05
  • @Quentin Yeah that looks like what is happening, is it possible to do what I'm trying through html alone? edit - the whole page I'm working on is the email – Ryan Mckenna Oct 13 '14 at 16:07
  • No. HTML is not a programming language. It can't generate numbers, interact with your database or send email. – Quentin Oct 13 '14 at 16:08
  • Why are you using `echo` here? Shouldn't that HTML and link be part of the mail sending code? – mario Oct 13 '14 at 16:08
  • @mario I'm trying to include php variable in a hyperlink, I can't add php variable into html but I can add html into php – Ryan Mckenna Oct 13 '14 at 16:09
  • Show your mail text generation code. That's where you need to add it. Something akin to `$mail_body .= "Click to Activate";` – mario Oct 13 '14 at 16:11
  • @mario Added it, it just uses the content.html (which is holding the contents of the email that is sent) – Ryan Mckenna Oct 13 '14 at 16:14
  • Well, `file_get_contents` does not run PHP code. Use a HEREDOC string or whatever instead. – mario Oct 13 '14 at 16:15
  • Do you think this is even possible? Needs php and obviously php won't work – Ryan Mckenna Oct 13 '14 at 16:16
  • Use a url with file_get_contents and request a php page - test it out by calling it directly first. Note that some servers disable url fetch with this function – cyberwombat Oct 13 '14 at 16:27

2 Answers2

0

Due to extreme difficulties I'm going down a different route. Instead of activating this way, I'm changing to this:

  • User registers
  • They get sent an email with a key
  • I will create a seperate page with a form to enter the key
  • If the key entered matches the key in the database then they're activated

Much more simple :)

Ryan Mckenna
  • 53
  • 1
  • 7
0
"verify.php?hash='.$hash.'"; 

Isn't Correct, you don't need the apostrophy and the dot to include the variable in a double quoted string. try

"verify.php?hash=$hash";

then do something like this mail($email, 'Please activate your account', "Hello " . $username. ",\r\nThank you for registering with us. Please visit the link below so we can activate your account:\r\n\r\nhttp://www.example.com/verify.php?hash=" . $hash . "\r\n\r\n-- Example team");

Then on you verify page use

S_GET['hash'] 

and check if it's correct against a database. I would also add the email in the url so you can check the email and code against each other, just in case you give out the same hash twice.

Billy
  • 2,448
  • 1
  • 10
  • 13
  • ps. If you're trying to send mail from xampp on your local computer you may need to do some setting up for it to work. Cant remember the exact steps but I'm fairly sure xampp won't send mail. try this SO question [SO Xampp mail Question](http://stackoverflow.com/questions/15965376/how-to-configure-xampp-to-send-mail-from-localhost) – Billy Oct 13 '14 at 17:40
  • I'm running a WAMP server with phpmailer, I can send mails fine, I just have problems getting data across (like I usually get the username from the logged in user's ID, but the user isn't logged in, they've only just registered) so it's really tough – Ryan Mckenna Oct 13 '14 at 17:56
  • I had it working from a code I got off a website, Give me 10 and I'll find the link – Billy Oct 13 '14 at 18:01
  • I've updated it a lot seeing as I couldnt get the original to work lol I've added the updated parts – Ryan Mckenna Oct 13 '14 at 18:04
  • Original site gone, Sunnytuts. I 've put them in a paste bin. The register page[Register page](http://pastebin.com/dtBNPdpC)first – Billy Oct 13 '14 at 18:09
  • And the activate page [Activate page](http://pastebin.com/qUeqXac1).It all works when it's up and running. shame the page is gone as it was a good intro into PDO – Billy Oct 13 '14 at 18:14
  • p.s, Just had another look through the site you posted, I would,nt send them their password back to them in their welcome Email, Just the username – Billy Oct 13 '14 at 18:20
  • the hash is seperate from the password, it is used to see if the account has been verified :) As much as I appreciate it, I don't really need/want full code written, my registration, users etc work fine, I just don't know how to get a variable from one page to another (see last edit :P) – Ryan Mckenna Oct 13 '14 at 18:30
  • Have you got a session running. You could set S_SESSION['user_var'] on one page and get value on other page. Other way to do it is add it to the url from one page as a query string then $_GET it on the other page, Or if you don't want users seeing it do it as a hidden form with the link as the submit button and the Var you want passed as a hidden input. – Billy Oct 13 '14 at 19:47
  • Yeah I've started using the sessions :) All is good besides displaying variable in the email (as file_get_contents doesn't include php), I don't think it is possible :( – Ryan Mckenna Oct 13 '14 at 19:59
  • The code I pasted in paste bin was just an example. all you need is the register page to take the email and make the code, then pass it to the register function in the user class then use the activate page for receiving the incoming code and email and set confirmed to 1 in your DB. Have a look at the REGISTER function in the user class – Billy Oct 13 '14 at 20:17
  • Just out of curiousity, try changing hash to another word as hash is a reserved word in sql. Try making it Email_code or something and obviously changing the field name in the DB aswell, PHP also has a hash function. These might be interfering with your code – Billy Oct 13 '14 at 20:50
  • I've got it working, I had to send the php code to a different file and use ob_start :) As gratification for your help I will give you +1s on your replies, many points for you! – Ryan Mckenna Oct 13 '14 at 21:12
  • Nevermind I don't have the reputation :( thanks anyway though! – Ryan Mckenna Oct 13 '14 at 21:14