PHP don't update time - php

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.

Related

PHP - Change Variable Name

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.

expiring php redirect on set date

I.m looking for a way in Php to have a Redirect expire on a said day.
The use for this is that I'm sharing file on a sever using the code below.
What I like is to have that redirect take them to a page that lets them know there time is up and they can see the Redirect anymore or tell I update the date of expire.
if (test){
---do something---
die("<script>location.href = 'http://file.here.com'</script>");
} else {
return false;
}
I have also try
function BeforeProcessList(&$conn, &$pageObject)
{
$target_date = new DateTime("05-01-2014");
$today = new DateTime();
if((int)$target_date->diff($today)->format('%r%a') >= 0)
{
header("Location: testview_list.php");
exit();
}
}
There are several scenarios for this case but i suggest to use session.
Use additional session to be taken once your privilege period is valid and make sure that you've already insert a date when a user get into that file along with value that indicate this user is allowed , for example,1 so you can change a value to ,for example,0 after specific period and that user will be denied automatically or redirected to another page
I got it to work.
<?php
// has this expired?
$expire_date = "2015-08-25"; // good until Aug 8/15
$now = date("Y-m-d");
if ($now>$expire_date) {
header("Location: http://yoursite.com/outoftime.html");
die();
}
// and now the unexpired part of the page ...
header("Location: http://yoursite.com/fileshare/");
die();
Thanks for the help guys

Simple PHP url Help needed

I am new to php, I need small help
I m creating a page in php named index.php
i want when someone view that page automatically a number or anything would be added to the url end
Like www.abc.com/index.php ---> www.abc.com/index.php?abc or ?132
when ever that index page is refreshed it should get a number or any variable in the end
Try this:
<?php
if (!isset($_GET["123"])) {
header("Location: " . $_SERVER["PHP_SELF"] ."?123");
}
$QS = $_SERVER["QUERY_STRING"];
$URL = "http://www.example.com/index.php";
// Check if Anything already assigned
if( empty($QS) ) {
// Generate Any RANDOM Number Here
$NUM = mt_rand(999, 9999);
// Reload Page and assign number
header("Location: {$URL}?{$NUM}");
}
Place this code in the very top of your page
Give this a go
<?php
$num = '123';
if(!isset($_GET[$num])){
header("Location: /path/to/page?$num");
}

Session variable loses value after redirection

I have three files.
File 1: index.php
which redirects to second.php and sends id=$var to second.php.
code:
$var=52013;
window.open('second.php?id=$var','name','width=1000,height=700,scrollbars=yes');
File 2: second.php
In this file I have received $var, when I print it in logs it displays value of $var.
<?php
session_start();
$_SESSION['id'] = $_REQUEST['id'];
$GLOBALS["log"]->fatal("Get id of order = ".$_SESSION['id']);
//Here it redirects to third file for some varification online.
//This condition is true first time when this page is called but after
//redirection from third.php this condition becomes false.So code below
//this if statement will be executed after redirection from third file.
if(condition==true)
{
header("Location: http://third.php");
}
print "Please visit:\n$authUrl\n\n";
print "Please enter the auth code:\n";
//After redirection from third.php i want to write document using value stored
//in session variable which we get at top.($_SESSION['id'])
$val = $_SESSION['id'];
//Write a file
$file_open = fopen("modules/doc/views/document.txt","wb");
$content = $val;
fwrite($file_open,$content);
fclose($file_open);
File 3: Third.php
This file do some varification online and then redirect to second.php in order to write session id in document.txt
So the problem is that when third.php redirects to second.php file the session variable loses its value. I want to write the value of session variable in document.txt after redirection from third.php but at that time $_SESSION['id'] contains nothing.
But the process should be same, I don't want to change it. It's the requirement.
i.e index.php -> second.php -> third.php -> second.php -> write session value.
Thanks
at the begining of the second.php you do this:
session_start();
$_SESSION['id'] = $_REQUEST['id'];
which is clears the session, and puts into $_SESSION['id'] whatever was received in $_REQUEST['id']. That is why you have nothing in $_SESSION['id'] when commint from third.php. Note that when you do this:
window.open('second.php?id=$var' ...
you just send string '$var' in id

How do I redirect to referring page/url after successful login?

I'm aware that this topic has been covered before here on Stack, and I have looked at some answers, but I'm still a bit stuck, being fairly new to PHP. Every page on my website requires a login, and so users are redirected to a login page on page load. At the top of each page then I have:
<?
require("log.php");
include_once("config.php");
include_once("functions.php");
?>
This redirects the user to log.php (with new code added):
<?
session_name("MyLogin");
session_start();
if(isset($_SESSION['url']))
$url = $_SESSION['url']; // holds url for last page visited.
else
$url = "index.php"; // default page for
if($_GET['action'] == "login") {
$conn = mysql_connect("localhost","",""); // your MySQL connection data
$db = mysql_select_db(""); //put your database name in here
$name = $_POST['user'];
$q_user = mysql_query("SELECT * FROM users WHERE login='$name'");
if (!$q_user) {
die(mysql_error());
}
if(mysql_num_rows($q_user) == 1) {
$query = mysql_query("SELECT * FROM users WHERE login='$name'");
$data = mysql_fetch_array($query);
if($_POST['pwd'] == $data['password']) {
$_SESSION["name"] = $name;
header("Location: http://monthlymixup.com/$url"); // success page. put the URL you want
exit;
} else {
header("Location: login.php?login=failed&cause=".urlencode('Wrong Password'));
exit;
}
} else {
header("Location: login.php?login=failed&cause=".urlencode('Invalid User'));
exit;
}
}
// if the session is not registered
if(session_is_registered("name") == false) {
header("Location: login.php");
}
?>
The login form is contained in login.php. The code for login.pho relevant to the PHP/log.php is:
<?
session_start();
if($_GET['login'] == "failed") {
print $_GET['cause'];
}
?>
and
<form name="login_form" id="form" method="post" action="log.php?action=login">
The answer that I came across stated that I should add:
session_start(); // starts the session
$_SESSION['url'] = $_SERVER['REQUEST_URI'];
to the top of each page, which I did, at the top of the page (above "require("log.php");"), and then add:
if(isset($_SESSION['url']))
$url = $_SESSION['url']; // holds url for last page visited.
else
$url = "index.php"; // default page for
to my login page, and use the following URL for redirect on successful login:
header("Location: http://example.com/$url"); // perform correct redirect.
I am not 100% where the code which stores the referring URL should go, at the top of log.php or login.php.
I have tried adding it to both, but the login page is just looping once I have entered the username and password.
I wonder if someone could help me get this working?
Thanks,
Nick
It appears that I don't have the privilege to comment on your post, so I'll do the best that I can to answer. I apologize for all of the scenarios, I'm just doing the best I can to answer on a whim.
SCENARIO 1:
If you've truly not selected a database in your code, as demonstrated here, could that potentially be your issue? Please do note, that the code below, is the code you've posted.
$db = mysql_select_db(""); //put your database name in here
SCENARIO 2:
The code below is not something I've ever used in anything I've built, might I suggest that you try replacing that line of code with the line below it?
if(session_is_registered("name") == false) { // Current
if(isset($_SESSION['name']) == false) { // Potential Replacement
SCENARIO 3:
If you're logic for the following, exists on the login.php file as well... That could potentially be your problem. Upon visiting your site, I noticed your form appears on login.php, yet your logic is posting to log.php. I'm hoping this bit of code can help rule out that "jump", as login.php might be saving itself and overwriting the $_SESSION variable you've established
session_start(); // starts the session
$_SESSION['url'] = $_SERVER['REQUEST_URI'];
If it's too complex to take it out of the login.php file, if you even have it there, I've put together some code that you can use to create "internal" breadcrumbs, so you can go 2 pages back in your history.
if(!isset($_SESSION['internal_breadcrumbs']))
$_SESSION['internal_breadcrumbs'] = array();
$_SESSION['internal_breadcrumbs'][] = $_SERVER['REQUEST_URI'];
$max_breadcrumbs = 5;
while(count($_SESSION['internal_breadcrumbs']) > $max_breadcrumbs)
array_shift($_SESSION['internal_breadcrumbs']);
That will create an array with a max of $max_breadcrumbs elements, with your most recent page at the end, like the following
Array
(
[internal_breadcrumbs] => Array
(
[0] => /other_page.php
[1] => /other_page.php
[2] => /other_page.php
[3] => /user_page.php <-- desired page
[4] => /login.php <-- most recent page
)
)
So now... you can setup your url to be something more like the following...
// I'm doing - 2 to accommodate for zero indexing, to get 1 from the current page
if(isset($_SESSION['internal_breadcrumbs']))
$url = $_SESSION['internal_breadcrumbs'][count($_SESSION['internal_breadcrumbs']) - 2];
else
$url = "index.php"; // default page for
All the best, and I certainly hope this has helped in some way.
IN SCENARIO 4
From the client test the login/password which ajax XMLHttpRequest with javascript code to a dedicated script for validation (do it on mode https for secure)
If response is right send the login password to your script server.
Stips : Encoding password is better secure !
Using header() function it's a bad idea.
Manual specification say ;
Remember that header() must be called before any actual output is
sent, either by normal HTML tags, blank lines in a file, or from PHP.
It is a very common error to read code with include, or require,
functions, or another file access function, and have spaces or empty
lines that are output before header() is called. The same problem
exists when using a single PHP/HTML file.
So in your case, i suggest that to use cookies with an ID generate only for the session, at the first connection its generate, and the duration of the cookie maybe for only from 2 to 10 minutes.
Regenerate cookie each time the loging.PHP is called !
Have a nice day

Categories