baconcheese113
baconcheese113

Reputation: 963

Embed PHP in an email to start a session and store variable

I'm trying to create a session variable when a user opens the email i send them. Everything is done with two .php files. (mailer.php and register.php)

mailer.php gets all the information for a user and sends them a email with a url to the register.php page.

I want to get a session variable (User_id) from mailer.php into register.php. I've put the following in the email that i send from mailer.php. I doubt it works, but it illustrates what I need.

`$message = $Url . "php session_start(); '$_SESSION[id]' = " . $row['user_id'] . " ?>";
...
$mail->MsgHTML($message);
$mail->Send();`

And then on the register page

`$userID = $_SESSION['id'];`

Upvotes: 1

Views: 1781

Answers (2)

Emz
Emz

Reputation: 1280

First off, php executes code once, and that is when the page loads.

With that said, correct me if I take your question wrong. You send a mail with an ID that ID you want to be able to fetch once someone opens the mail? I would recommend a regexp or a simple string substr ( string $string , int $start [, int $length ] ) to fetch it from the mail.

If you get the mail as raw text (which you will under normal circumstances) then just grab it.

Example mail:
"Hello12ID:5 My name is..."

A while ago I did handle strings like this, but here it goes.

$id = substr($email, strpos($email,'ID:'), strpos($email, ' ', strpos($email,'ID')));
if (is_numeric ($id)) echo 'failed: '.$id.' is not numeric'; exit;

What it does is that it returns what is between 'ID' and the space therafter. In my case it is 5. So $id = 5 in this case. Then you just save that 5 into a session, which is possible to do at once as well, but better check so it grabs something okay before.

Hope it was what you meant.

Upvotes: 0

Johndave Decano
Johndave Decano

Reputation: 2113

You cannot attach function inside of the variable...Instead just create new table to store verification code,

function genRandomString() {
    $length = 10;
    $characters = ’0123456789abcdefghijklmnopqrstuvwxyz’;
    $string = ”;    
    for ($p = 0; $p < $length; $p++) {
        $string .= $characters[mt_rand(0, strlen($characters))];
    }
    return $string;
}
$code = genRandomString();
$message = "http://localhost/start_session.php?verify=$code";
$mail->MsgHTML($message);
if($mail->Send()){
    echo 'Message Sent';
    // Then execute a query to insert the generated code to your database
}

When user opens his email and click the link, hell go to the verification page.

Match the verification code in the database using get variable and if found, start the session otherwise no session 
$code = mysql_real_escape_string($_GET['verify'];
$query = mysql_query("SELECT * FROM verifications WHERE verification_code=$code");
$rows = mysql_num_rows($query);
if($rows > 0){
    session_start();
    session_regenerate_id();
}

Upvotes: 2

Related Questions