Emailing php output - php

I have built a jquery mobile form that submits to a php file. I want the following php output to also have the option of emailing it to the user.
<?php
$DCity=$_GET["DCity"];
$ACity=$_GET["ACity"];
date_default_timezone_set($DCity);
$DZone = date("Y-m-d h:i:s A");
date_default_timezone_set($ACity);
$AZone = date("Y-m-d h:i:s A");
echo ((strtotime($DCity) - strtotime($ACity))/3600)." hour Timezone Difference</br>";
?>
This will output something like "5 hour Timezone Difference".
Below this php I have a this code which should save the output for an email.
<?php
ob_start();
$DCity=$_GET["DCity"];
$ACity=$_GET["ACity"];
date_default_timezone_set($DCity);
$DZone = date("Y-m-d h:i:s A");
date_default_timezone_set($ACity);
$AZone = date("Y-m-d h:i:s A");
echo ((strtotime($DCity) - strtotime($ACity))/3600)." hour Timezone Difference</br>";
$var=ob_get_flush();
?>
The user then has the option to email this to themselves. If they click the link it takes them to a new form to enter in their email address which posts to a new php that contains this code.
<?php
$to = $_GET["email"];
$subject = "Your Personalized JetLag Pilot Plan";
require 'OriginalOutput.php';
$body = $var;
mail($to,$subject,$body);
echo "Mail sent to $to";
?>
The original php output is working correctly. However whenever I send an email that should contain the same output, it always says "0 hour Timezone Difference." The email form is working correctly just not carrying the OriginalOutput.php. Anyone know where my mistake is?

Related

I need help setting up a new log file

I can successfully tail a message log that my web developer friend setup. Now I have another PHP script that I need to create a log for to help troubleshoot a problem. I copied the code my friend came up with and put it into my new PHP file:
function log_message($message, $type) {
$date = date('m/d/Y h:i:s a', time());
$message = "$date - $type - $message \n";
error_log($message, 3, '/var/log/router');}
Then in various places throughout my code I added lines similar to the following:
log_message("Email to $recipient: was successfully sent.", $info);
I duplicated a log file in /var/log, renamed the file to router and then deleted the existing log information that was in it so that its empty. This log file code works great on the PHP page that my friend created, but I can't get it to work on the page I am troubleshooting.
In the past I simply stored debugging messages in the session, but I'm branching out and trying to make use of log files.
Maybe it is because of the escaping?
function log_message($message, $type) {
$date = date('m/d/Y h:i:s a', time());
$message = $date . " - " . $type . " - " . $message . "\n";
error_log($message, 3, '/var/log/router');
}
Does that work? Hope to help

PHP: If $date is today, send an email - but only send it once

I'm trying to add something to my website where:
If the date is a certain date, I want it to send me an email; however every time I refresh the page on that day it resends the email. I don't want it to do this - only send the email once.
I know a little about SESSIONS and such but not enough to implement them. This is my code, any help would be appreciated.
TL;DR - How to send an email from the website once if the date is a set date?
//Inspections
<?php
$recipients = array("Fake#email.com","Another#FakeEmail.com");
$to = implode(",", $recipients);
$subject = "Inspection Reminder.";
$bedMsg = "There is a BEDROOM inspection in five days!";
$comMsg = "There is a KITCHEN inspection in five days!";
$dateInspect = date("05/11/16"); //Set to a certain date for testing
if ($dateInspect == "05/11/16"){
mail($to,$subject,$comMsg);
} elseif ($dateInspect == "26/11/16"){
mail($to,$subject,$comMsg);
} elseif ($dateInspect == "24/11/16"){
mail($to,$subject,$bedMsg);
} else {
;
}
?>
The better solution is to use an database to store the flag of sending the email on a specific day.
But, if you running a simple website and don't want to go for database, you can simply create a file after the sent, and always check if that file exists.
$dateInspect = date("05-11-16");
if(!file_exists("mail_sent_".$dateInspect.".txt")){
// .. SEND YOUR E-MAIL
// create the file
file_put_contents("mail_sent_".$dateInspect.".txt", "mail sent this day");
}
Attention: Don't use slashes on date, as it will generate an filename as: mail_sent_05/11/16.txt which is an invalid name, filenames can't have slashes.

Make Link expire after X minutes

Okay I am sending an activation email link to user's email. My Activation link is working perfect I wants to make that link expire after X minutes.
Here is my code:
$base_url='http://172.16.0.60/WebServices/';
$activation=md5($email.time());
email format to HTML
$mail->Subject = 'Activate Your Account';
$mail->Body = 'Hi, <br/> <br/> We need to make sure you are human. Please verify your email and get started using your Website account. <br/> <br/> '.$base_url.'activation/'.$activation.'';
Now my
.htaccess file
RewriteEngine On
RewriteRule ^activation/([a-zA-Z0-9_-]+)$ activation.php?code=$1
RewriteRule ^activation/([a-zA-Z0-9_-]+)/$ activation.php?code=$1
Now how can I make my link expire? I dont have timestamp field in my database.
I dont have timestamp field in my database.
Then add one.
Add a field to record when the link was generated and you can then instantly check against that time when they visit. There is no need to waste time trying to think of hacks when you can simply adjust your setup accordingly.
I got my answer using this simply,
Send a timestamp in link and on other end while accepting the link I have to check the time with current time stamp.
$base_url='http://172.16.0.60/WebServices/';
$activation=md5($email.time());
$time = time();
email format to HTML
$mail->Subject = 'Activate Your Account';
$mail->Body = 'Hi, <br/> <br/> We need to make sure you are human. Please verify your email and get started using your Website account. <br/> <br/> '.$base_url.'activation/'.$activation.$time.'';
While when user clicks on link, following code will check the expiration of link.
$u_time = $this->input->get('time'); // fetching time variable from URL
$u_time = md5($u_time);
$cur_time = time(); //fetching current time to check with GET variable's time
if ($cur_time - $u_time < 10800)
{
// link is not expired
}
else
{
// link has been expired
}
date_default_timezone_set("Asia/Kolkata"); // set time_zone according to your location
$expire_date = "2019-03-26 14:45:00"; // time that you want the link to expire
echo $now = date("Y-m-d H:i:s"); // your current time.
if ($now>$expire_date) {
echo " Your link is expired";
}
else
{
echo " link is still alive";
}
you can find your time_zone here
Hope this helps:)

Passing a timestamp through a PHP form, and formatting it on second page

This is a very basic project for a PHP class I am in - no databases involved so the data isn't going anywhere except the next page.
Currently, I am formatting the date on the page with the form using a hidden field, and it gets passed through in the URL post-formatting (with all the spaces punctuation). That satisfies the requirements of the assignment, but does not seem like the cleanest way to do this.
The form is "Comp2.php" and the page that displays the data is "Comp2b.php"
Here are the code segments in question:
(There are other fields in play of course, but I left them out since they are just text)
Comp2.php
<?php
if (isset($_POST['submit'])) {
$valid = true;
$dateadded = $_POST['dateadded'];
if ($valid) {
header("Location: Comp2b.php?albumid=$albumid&album=$album&artist=$artist&dateadded=$dateadded");
exit();
}
} else {
$albumid="";
$artist="";
$album="";
$price="";
$type="";
$playlists[0]="";
$genre="";
$tracks="";
}
?>
<form method="post" action="Comp2.php">
<?php $currentDate = date('l, F d, Y h:i:s a.') ?>
<input type="hidden" name="dateadded" value="<?php echo $currentDate; ?>">
Then the rest of the form continues...
This is
Comp2b.php:
<?php
echo "Album ID: <strong>";
echo $_GET['albumid']."</strong><p><strong>".$_GET['album']."</strong> by <strong>".$_GET['artist']."</strong> added on ".$_GET['dateadded'];
?>
How can I pass something like date() or time() through the form, and do the formatting i.e., date('l, F d, Y h:i:s a.', $dateadded) on my second page instead of passing a full string through the URL?
In the first page put a simple time() in the hidden field
<input type="hidden" name="dateadded" value="<?php echo time(); ?>">
Then in the second page output that timestamp formatted any way you like
<?php
echo "Album ID: <strong>";
echo $_POST['albumid'];
echo '</strong><p><strong>'.$_POST['album'];
echo '</strong> by <strong>' . $_POST['artist'];
echo ' </strong> added on ' . date('l, F d, Y h:i:s a.', $_POST['dateadded']);
?>
Reasons:
time() generates a simple number like 876243672834 which is much easier to pass around and less likely to cause confusion.
'date()` can have 2 parameter, the first is a format, and the second is a timestamp representing a date and time in unix format.
Also because your form <form method="post" has a method of post the data will end up in the $_POST array and not the $_GET array
The $_GET array is populated when you do things like <a href="xxx.php?a=1&b=2"> or use <form method="GET"

js getTimezoneOffset() in php

new Date().getTimezoneOffset()*(-1);
This JS function returns the UTC Time Zone of the machine in minutes. Is there any PHP function does the same thing?
Thank you..
<?php // RAY_easy_client_time.php
error_reporting(E_ALL);
// USE JAVASCRIPT TO GET THE CLIENT TIME AND COMPUTE THE OFFSET FROM THE SERVER TIME
// LOCATION OF THE SERVER - COULD BE ANYWHERE
date_default_timezone_set('America/Denver');
// DIFFERENCE OF SERVER TIME FROM UTC
$server_offset_seconds = date('Z');
// WHEN THE FORM IS SUBMITTED
if (!empty($_POST))
{
// JAVASCRIPT TELLS US THE CLIENT TIME OFFSET FROM GMT / UTC
$client_offset_minutes = $_POST["date_O"];
$client_offset_seconds = $client_offset_minutes * 60;
// THE TIME WE WANT AT THE CLIENT LOCATION
$client_timestring = 'TODAY 7:00AM';
// MAKE THE COMPUTATIONS, INCORPORATING THE OFFSET FROM GMT
$client_timestamp = strtotime($client_timestring) + $client_offset_seconds;
$server_timestamp = $client_timestamp + $server_offset_seconds;
$server_timestring = date('l, F j, Y \a\t g:i a', $server_timestamp);
echo "<br/>ACCORDING TO THE VALUE FROM PHP date Z";
echo "<br/>SERVER IS LOCATED $server_offset_seconds SECONDS FROM UTC";
echo "<br/>";
echo "<br/>ACCORDING TO THE VALUE FROM JS dateObject.getTimezoneOffset()";
echo "<br/>CLIENT IS LOCATED $client_offset_minutes MINUTES FROM UTC";
echo "<br/>";
echo "<br/>WHEN IT IS '$client_timestring' AT THE CLIENT, IT IS '$server_timestring' IN " . date_default_timezone_get();
}
// END OF PHP - USE HTML AND JS TO CREATE THE FORM
echo PHP_EOL; ?>
<form method="post">
<input name="date_O" id="dateTime_O" type="hidden" />
<input type="submit" value="CHECK CLIENT DATETIME" />
</form>
<!-- NOTE THIS WILL GIVE YOU THE VALUES AT PAGE-LOAD TIME, NOT AT SUBMIT TIME -->
<!-- MAN PAGE REF: http://www.w3schools.com/jsref/jsref_obj_date.asp -->
<script type="text/javascript">
var dateObject = new Date();
document.getElementById("dateTime_O").value = dateObject.getTimezoneOffset();
</script>
PHP, being server side, can only read what the client sends. It cannot tell the user timezone without being explicitly told by the user (or a user agent like the browser).
There are a couple ways to guess. You can look up the IP and guess timezone based on IP location by calling a location service (time expensive and can return incorrect results, I wouldn't recommend it).
You can also have your javascript make an ajax call to tell the server what the value for Date().getTimezoneOffset() is. Personally I would take this route.

Categories