Reputation: 886
Lol, it seems that this question has not ever been asked before in google.
So, what I am trying to do is to send email via php form. The scenario of this is the registered member will invite thier friend to our page by typing as many as email address to the text area. When they submit the form, the form will send email to the all email address they enters in the text area.
This email actually is a news letter. Where in the email there would be some products to show to the addressed email.
What I want to do with the $_SESSION in the email is: when the people who has the email adress click one of products in the news letter and come to the page, the page will get from whom he got the link to the page.
[EDIT]
I dont want to achieve it with a string like this: index.php?email=$email&username=$username
since this can only be set to one link not global link and this can also be manipulated by the new coming people.
What I mean by global link is, as what I mentioned above, there would be many products I am going to represent in the newsletter, such as ../product-a.php, ../product-b.php, ../product-c.php, ../product-d.php, and so on. And it will make us to create many different sessions in all pages because all user who have clicked one of the product will explore the website first before deciding to buy the product or register to the website.
So, here is my code to set the session:
<?php
$kemail=$_POST['to'];
$to = "$kemail";
$referal_identity = "dsfsdf";
$referal_address = "[email protected]";
$_SESSION['referal_identity']=$referal_identity;
$_SESSION['referal_address']=$referal_address;
$subject = 'Your Friend is good';
// message
$message = '<html>
<head>
<title>From our company</title>
</head>
<body>
<p>Your friend invite you. Click this <a href="index"> link </a> </p>
</body>
</html>';
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= 'From: Klaudia <[email protected]>' . "\r\n";
mail($to, $subject, $message, $headers);
?>
<form action="" method="post">
Email: <textarea name="to"></textarea>
<input type="submit" name="submit" value="send">
</form>
<?php
session_start();
$referal_identity=$_SESSION['referal_identity'];
$referal_address=$_SESSION['referal_address'];
?>
<body>
<?php echo $referal_identity ?>
<?php echo $referal_address ?>
<p><a href="test-referal.php">test-referal.php</a></p>
</body>
If we just created a session in the email, then we can call it in one time in all page for all links of webpage. That's why I want to do it with SESSION, or something like that.
Upvotes: 0
Views: 871
Reputation: 6393
you are having some misconceptions.
the session won't get carried into the email. the easiest way to serve the purpose is.... yes, a token.
before mail()
generate a token. (any unique random string will do)
while generating the links e.g. <a href="http://example.com/product1">Product 1</a>
you will add the token like this:
<a href="http://example.com/product1?token=$token">Product 1</a>
this step is also before sending the mail. you need to save that token against the email address somewhere. e.g.
ID email token
1 [email protected] abcdefg1
2 [email protected] abcdefg2
after that, send the mail
when someone comes to your site following this token, you can check it against the above table and can know, from which email he has come.
Upvotes: 0
Reputation: 218960
Sessions are used to track a particular user visiting the website for a relatively small frame of time. They don't do what you're trying to do.
Take a look at the link in the email:
Click this <a href="index"> link </a>
That link is all you have for transferring data from that email body back to the website. So that link is going to need to carry some sort of identifier.
When a user creates these referrals, generate some kind of token. Any unique string will do. Save that token in your database to identify the information about that referral. This can include any information you want to track. Then include that token in the email:
Click this <a href="http://somewebsite.com/index?referral=' . urlencode($someToken) . '"> link </a>
So the user would click on this:
Click this <a href="http://somewebsite.com/index?referral=abc123"> link </a>
Now when this user clicks on the link to visit your site, you can get that token:
$_GET["referral"]
And you can use that value to check the database for any of the data you're tracking. This will tell you which referral brought that particular new user to the site.
You can generate multiple tokens for multiple purposes along how ever many dimensions you want to track for referrals.
Upvotes: 1