I have two 'includes' php pages, Page 1 and Page 2.
Page 1 displays contents for today, Page 2 displays contents for yesterday.
I set a cronjob to copy contents of Page 1 to Page 2 every 0:00am daily.
On the main page (the page that contains this page 1 and page 2), I have:
$date = date("d/m", strtotime("now"));
$dateyes = date("d/m", strtotime("-1 day"));
Then on Page 1, I have:
echo $date;
On Page 2, what I need is:
echo $dateyes;
But due to the cronjob, I only get
echo $date;
on that Page 2. Is there a way I can change that $date to $dateyes on that Page 2?
Not really sure how copying a php script from day to day should work in practice, but if need this pattern for some reason, simply replace your current echo $date; to echo $is_page1 ? $date : $dateyes;
Where $is_page a variable that check which page is loaded via something like:
$is_page1 = $_SERVER['SCRIPT_NAME'] === 'page1.php';
Or:
$is_page1 = basename(__FILE__) === 'page1.php';
I found a solution.
On the mainpage.php, I checked if "dateyes" already exists in page2.php, if it already exists, script does nothing but if "dateyes" does not exist, my script searches for "date" in that page2.php and since what I needed on page2.php was "dateyes", I replaced "date" with "dateyes".
On mainpage.php
$datetod = date("d/m", strtotime("now"));
$dateyes = date("d/m", strtotime("-1 day"));
$file = file_get_contents('../includes/page2.php');
if(strpos($file, "dateyes") !== false){}
else {
$str=str_replace("datetod", "dateyes",$file);
file_put_contents('../includes/page2.php', $str);
}
So I no longer worry about the cronjob which automatically copies $date from page1.php to page2.php everyday at 0:00am because the first person who loads/views the page (mainpage.php) is automatically going to change the $date in page2.php to $dateyes which is what I need.
Related
i redirect to a page in the same folder using onclick event to show information for different dates , but when i use onclick then it doesn't work , it only works if make a button and put everything in side a form , don't want to use buttons because it doesn't fit the style of the page. Is my code wrong or do i have to do some setting to keep the session alive.
Page A
<php
//enable sessions
session_start();
$todaydate=new datetime();
$todaydate=$todaydate->format("Y-m-d");
//check if session is not set
if(!isset($_SESSION['curdate'])){
$_SESSION['curdate']=$todaydate;
}
// get monday's date
$daynumber=date('N',strtotime($todaydate));
$daynumber=$daynumber - 1;
$mondaydate=date('Y-m-d',strtotime($todaydate . ' - '.$daynumber.' day'));
$_SESSION['mondaydate']=$mondaydate;
$i=0;
While($i != '7'){
$buttondate=date('Y-m-d',strtotime($mondaydate. ' + '.$i." day'));
echo '<span onclick="location=','B',$i,'.php'",'">',$buttondate,'</span>';
$i++;
}
?>
Page B1
<?php
session_start();
$choosedate=$_SESSION['mondaydate'];
$$_SESSION['curdate']=$choosedate;
include_once("a.php""):
?>
first time Question asker here. I'm building a web page and want to change the text on page if the user has visited certain pages.
My initial idea was too create an array in the session which records each url as its visited with something like $_SERVER['REQUEST_URI'], which would probably work if it was a site I was building from scratch. however... Because the site is built in Wordpress I'm not 100% how to go about doing that within there system.
Now here's what I'd suggest you do. Navigate to your Theme and open the file functions.php
Then find a suitable spot anywhere on the File (The bottom of the file wouldn't be that odd).
Then, add the following functions:
<?php
// FILE-NAME: functions.php <== LOCATED AT THE ___/wp-content/themes/your-theme-name
add_action("init", "initiatePageLogging");
function initiatePageLogging(){
// START THE SESSION IF IT HAS NOT BEEN STARTED
// THIS WOULD BE USED TO SHARE DATA ACROSS YOUR PAGES...
if (session_status() == PHP_SESSION_NONE || session_id() == '') {
session_start();
}
// CHECK THAT THE SESSION VARIABLE FOR OUR PAGE-LOGGING IS THERE
// IF NOT CREATE IT
if(!isset($_SESSION['visitedPages'])){
// IT DOES NOT EXIST SO WE CREATE IT & INITIALIZE IT AS AN EMPTY ARRAY
$_SESSION['visitedPages'] = array();
}
// NO NEED TO KEEP THE SESSION ARRAY $_SESSION['visitedPages'] TOO LONG
// SO WE TRIM IT OUT CONDITIONALLY TO KEEP IT UNDER CHECK
if(count($_SESSION['visitedPages']) >= 10){
// WE REMOVE ABOUT 7 ELEMENTS FROM THE BEGINNING OF THE ARRAY
// LEAVING JUST THE LAST 3 - NOW THAT'S COOL...
$arrVisitedPages = $_SESSION['visitedPages'];
array_splice($arrVisitedPages, 0, 7);
// RE-DEFINE THE $_SESSION['visitedPages'] ARRAY
$_SESSION['visitedPages'] = $arrVisitedPages;
}
}
function getLastVisitedPage(){
$lastVisitedPage = get_site_url() . $_SERVER['REQUEST_URI']; //<== FALL BACK TO THE CURRENT PAGE IF WE HAVE AN ISSUE.
if( isset($_SESSION['visitedPages']) && is_array($_SESSION['visitedPages']) ){
$arrVP = $_SESSION['visitedPages'];
$intArrVPLength = count($arrVP);
$diff = ($intArrVPLength - 2);
$lastVisitedPage = ( $intArrVPLength > 1) ? $arrVP[$diff] : $lastVisitedPage;
}
return $lastVisitedPage;
}
?>
Now, Part 1 is done! Inside of your Theme, You will still find a File Called header.php
This is where we have to do the logging because by default every page in Word-Press loads this Page (except when configured otherwise).
At the very, very top of that File - I mean on Line 1, do this:
<?php
// FILE-NAME: header.php <== LOCATED AT THE ___/wp-content/themes/your-theme-name
// BUILD THE URL OF THE CURRENT PAGE & PUSH IT TO THE SESSION VARIABLE...
$thisPage = get_site_url() . $_SERVER['REQUEST_URI'];
$_SESSION['visitedPages'][] = $thisPage;
// THAT'S ALL! BELOW HERE, THE ORIGINAL CONTENT OF THE header.php FILE CONTINUES...
?>
One more thing! How do we now use the $_SESSION['visitedPages'] Variable?
Otherwise, how do we know which page was last visited using the $_SESSION['visitedPages'] Variable?
Now on every File like (page.php, index.php, category.php, taxonomy.php, etc); you can now find out the last visited Page by doing something like this:
<?php
// FILE-NAME: ANY FILE IN THE THEME LIKE: page.php, index.php, category.php, taxonomy.php, etc
$lastVisitedPage = getLastVisitedPage();
// THAT'S IT, PAL...
?>
I hope this helps....
I made php code to add something like posts on side and above is time of adding it but only first post are getting updated time but next posts have time of first one (first are getting 16:35 and next have 16:35 also) here is code of data.php
session_start();
$dzien = date("d");
$mies = date ("m");
$rok = date("Y");
$_SESSION['data'] = $dzien.".".$mies.".".$rok;
$godz = date("G");
$min = date("i");
$_SESSION['czas'] = $godz.":".$min;
header("Location: edit.php");
exit();
And here is file.php which add posts and time:
session_start();
header("Location: data.php");
$tekst ="<div class='wpis'><div class='data' ><p class='tdata'>".$_SESSION['data']." ".$_SESSION['czas']."</p></div><div class='klaska'>". $_POST['pole']."</div></div>";
$fp = fopen("inne.txt", "r");
$stare = fread($fp,filesize("inne.txt"));
fclose($fp);
$ntekst = $tekst.$stare;
$fp = fopen("inne.txt", "w");
fputs($fp,$ntekst);
fclose($fp);
header("Location: index.php");
exit();
I want to get in next posts updated time of add
Line 2 of file.php prepares a header to redirect to data.php, but at the end of the file you prepare another header to redirect to index.php. See comment by shutout2730 at yahoo dot com in link:
Likely the first redirect is replaced by the later one and hence does nothing. There appear to be exceptions based on output echo, print and buffering.
Are you running data.php from someplace else to initialise it?
I suspect data.php is not called as you expect and hence session is not being updated.
I have a PHP page that takes a date variable: mypage.php?date=2015-07-14 and displays output.
How do I check if the date= is empty and if it is then insert and refresh the page with the current date?
I've tried to formulate a small script at the top of the page using _GET with the date value but am not sure how to handle the reload of the page?
You can simply check your input is not empty using empty function, and create a date (it will default to current, unless you pass parameter), and passed it as get parameter in header function to the same page.
if(empty($_GET['date']))
header('location:'.$_SERVER['PHP_SELF'].'?date='.date('Y-m-d'));
You can simply use the empty language construct from PHP to check if a date was supplied: http://php.net/empty
Then you could use the header-function to redirect if no date was entered in the url: http://php.net/manual/en/function.header.php
For example:
<?php
if (empty($_GET['date'])) {
header('location: ' . $_SERVER['REQUEST_URI'] . '?date=' . date('Y-m-d'));
}
Also note that any headers should be sent before generating any output in your script.
What you seek is a combination of isset and empty functions with redirect using header. The end result could look something like this
<?php
$rawDate = isset($_GET['date']) && !empty($_GET['date']) ? $_GET['date'] : false;
$requestDateTime = $rawDate ? DateTime::createFromFormat('Y-m-d', $rawDate) : false;
$currentDateTime = new \DateTime();
// If datetime from GET is invalid or is not current date...
if (!$requestDateTime || $currentDateTime->diff($requestDateTime)->days > 0) {
// Redirect to current page with current date
header(sprintf('Location: ?date=%s', $currentDateTime->format('Y-m-d')));
}
I have the following declaration at the beginning of my PHP script:
$GLOBALS['monthselect'] = date('m');
$GLOBALS['yearselect'] = date('Y');
during the script I assign the following values:
$GLOBALS['monthselect'] = $_GET['mo'];
$GLOBALS['yearselect'] = $_GET['yr'];
Next, after a form submits, I want to redirect it to the same selection in GET. (This is all in the same PHP script)
header('Location: ?yr='. $GLOBALS['yearselect'] .'&mo=' . $GLOBALS['monthselect']);
Problem is, this always reloads the page with the date of today. Never the newly stored values. So always this output:
website.com/?yr=2013&mo=06
What am I missing here?
Could you please provide the places where do you set $GLOBALS['monthselect'] = $_GET['mo']; and $GLOBALS['yearselect'] = $_GET['yr']; Make sure they get executed..