Website doesn't display pages as requested until Control+F5 - php

I have a strange situation here.
I had coded a webpage in php. It works fine throughout the day however if I close it down and try to access it again the following day it doesn't quite work as needed.
For example:
I have a main login page. When the user logs in the URL changes to the following page but it still shows the login input.
if i manually enter the URL again and press CTRL+F5 I get the original page. If I then press on the logout page which calls the logout.php the URL does change but it still shows me the index page until I manually entere the URL and press CTRL+F5 to force refresh.
once all the pages have been accessed this way the website they starts working fine again.
I have put headers to not cache the webpages but that is not helping here.
It happens with all browsers.
Any advice or am I sounding a bit stupid here?
header("Cache-Control: no-cache, must-revalidate");
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
session_start();
if(isset ($_SESSION['user']))
{
echo "Logged in as:".$_SESSION['user'];
echo "Home";
echo "Logout";
exit;
}
else
{
echo "<FORM name=\"login\" action=\"index.php\" method=\"POST\" onsubmit=\"return validatelogin(this);\">";
echo "<input type=\"text\" placeholder=\"Username\" name=\"username\">";
echo "<input type=\"password\" placeholder=\"Password\" name=\"password\"> ";
echo "Forgot my password";
echo "<input type=\"submit\" name=\"submit\" value=\"Submit\"> ";
echo "</FORM>";

Related

jQuery send data AJAX after the time is up

So, I have a form that has to be filled by the user in a duration. (Fyi, it's still a dummy web). Let's say the duration is 100sec. After 100sec, the data should be sent to database. Here are my codes :
HTML
<div id="countdown"></div>
<form class="test" method="post">
<?php
for($num=1;$num<=10;$num++){
echo "<div class=\"question\">" .$num. "<br>"; /* the question is written here */
echo "<input type=\"radio\" name=\"answer" .$num. "\" value=\"A\">A<br>";
echo "<input type=\"radio\" name=\"answer" .$num. "\" value=\"B\">B<br>";
echo "<input type=\"radio\" name=\"answer" .$num. "\" value=\"C\">C<br>";
}
?>
</form>
Why did I do PHP and looping? Coz this is the prototype of my form and also for shortening purpose. I'll not do this for the real web. But, if you think this is a bad idea although it's just a dummy web, just tell me :)
jQuery
var duration=100;
var countdown=setInterval(timer,1000);
function timer(){
duration=duration-1;
$("#countdown").html(duration+" sec");
if(duration<=0){
clearInterval(countdown);
var form_data=$(".test").serialize();
$.post("action.php",form_data,function(){alert("success!")});
}
}
action.php
<?php
include("connection.php"); /* it's the connection to the database */
$answer1=$_POST["answer1"];
$answer2=$_POST["answer2"];
$answer3=$_POST["answer3"];
$answer4=$_POST["answer4"];
$answer5=$_POST["answer5"];
$answer6=$_POST["answer6"];
$answer7=$_POST["answer7"];
$answer8=$_POST["answer8"];
$answer9=$_POST["answer9"];
$answer10=$_POST["answer10"];
mysqli_query($link,"INSERT INTO prototype (`answer1`,`answer2`,`answer3`,`answer4`,`answer5`,`answer6`,`answer7`,`answer8`,`answer9`,`answer10`) VALUES ('$answer1','$answer2','$answer3','$answer4','$answer5','$answer6','$answer7','$answer8','$answer9','$answer10')";
?>
So, the problem is : after the duration = 0, the success action (alert) appears, but after I checked the database, no data has been stored. Wut's actually happen?

Passing variables between two .php pages

On the catching page i have
$storeID= $_REQUEST['store'];
$custID= $_REQUEST['cust'];
I have links on different pages to the above page like the following
on page 1.
echo "<a class='btn btn-info' rel='nofollow' target='_blank' href='/go-to-store.php?store=" . $KID . "&cust=" . $user_ID . "'>" . "shop" . "</a>";
on page 2.
echo "<form action='/go-to-store.php' target='_blank' method='post'>"
. "<input type='hidden' name='store' value='$postid'>"
. "<input type='hidden' name='cust' value='$user_ID'>"
. "<button class='btn btn-mini btn-info' type='submit' style='margin-top:5px;'>"
. "shop" . "</button>";
Are both of these ways valid acceptable way of doing things. I have no reason not to show the values to user and there is no harm or motive for anyone to alter these values.
I'm tracing a problem on my site and basically narrowing down any possible scenarios where there could be even a remote chance of something causing the problems i have.
The $storeID looks like it contains data that is relevant only to a specific set of pages on the site, but which people might want to link to. It should form part of the URL so that people can link to it.
The $custID looks like it contains data specific to the user, but which should persist across the entire site (and isn't going to change unless the user logs out). It should be stored in a cookie (or stored on the server in a session which is associated with a browser with a cookie) and not passed in each request.
use cookies or session to store data and use them any page you want.
<?php
session_start();
$_SESSION['storeID'] = $_REQUEST['store'];
$_SESSION['custID'] = $_REQUEST['cust'];
// now access session variables from other pages
echo "Store ID : ". $_SESSION['storeID']."<br />";
echo "Cust ID". $_SESSION['custID']
// to delete variable
unset($_SESSION['storeID']);
unset($_SESSION['custID'])
?>
with cookies
<?php
// store varible in cookie for one day
setcookie("soreID", $_REQUEST['store'], 60*60*24);
setcookie("custID", $_REQUEST['cust'], 60*60*24);
// retrive data
echo "Store ID : ". $_COOKIE['storeID']."<br />";
echo "Cust ID". $_COOKIE['custID']
// delete cookie
setcookie("soreID", $_COOKIE['store'], time()-3600);
setcookie("custID", $_COOKIE['cust'], time()-3600);
?>
Option 1: Encode variables by base64 so your users can't see them without decoding. Put variables into url.
$storeID= urlencode(base64_encode($_REQUEST['store']));
$custID= urlencode(base64_encode($_REQUEST['cust']));
// then use following to get data from url:
$storeID= base64_decode(urldecode($_GET['sid']));
$custID= base64_decode(urldecode($_GET['cid']));
Option 2: Use sessions. Sessions allow you to save data to server, so they won't show to users.
session_start();
$_SESSION['storeID'] = $_REQUEST['store'];
$_SESSION['custID'] = $_REQUEST['cust'];
// then use following to get data from sessions:
session_start();
$storeID= $_SESSION['storeID'];
$custID= $_SESSION['custID'];
Option 3: Use text files. Keep in mind; i do not recommend this option because it loads server a bit and it doesn't beat other options. (you can use dbs also)
file_put_contents()
file_get_contents()
Edit:
session_start();
if(isset($_POST['store']) && isset($_POST['cust'])){
$_SESSION['storeID'] = $_POST['store'];
$_SESSION['custID'] = $_POST['cust'];
}
else {
echo 'please fill are fields.'
}

Input from PHP generated form is not being recorded

The issue I have been experiencing deals with input from HTML which was generated using PHP echo statements. Here is the function I have that outputs the form:
function confirm_recipients()
{
echo "<form action = ' ' name ='email_options' method='post'>";
echo "<input type='submit' name='sendRecipients' value='Yes, I want to send the emails' >";
echo "</form>";
}
Later on in the same PHP page, I call the function and then check to see if the submit button was set.
confirm_recipients();
if (isset($_POST['sendRecipients']))
{
//perform actions
}
However, this code is not functional seeing as even when the submit button is set (clicked by the user), the if statement block is never executed. Perhaps there is an issue with posting from the same file I want to "read in" from? Thanks for any advice.
Updates
Thank you for such immediate response. Sadly none of the suggestions have worked (removing the space in the action value or the suggestion made by user623952). No errors have been reported, the button is just failing to be set. I am looking for other places in the file that might have errors, perhaps in the order I call the function.
This works fine for me:
<?php
print "<pre>".print_r($_POST,true)."</pre>";
confirm_recipients();
function confirm_recipients() {
echo "<form action = ' ' name ='email_options' method='post'>";
echo "<input type='submit' name='sendRecipients' value='Yes, I want to send the emails' >";
echo "</form>";
}
if (isset($_POST['sendRecipients']))
{
print "<br/>sendRecipients is set!<br/>";
}
?>
I think your problem might be somewhere else in the code.
It's okay to POST the form data to the same script that contains the form. Change the action attribute to the URL of the script, do not set it to whitespace, which is what you did.
I don't think the value of a submit input is sent as part of the POST. Try using an input type="hidden" with the name 'sendRecipients'.

html always fetch fresh page from link

I'm setting a cookie on one page and trying to unset on the next page, which I reach via a link. However, the cookie only gets unset when I refresh the second page, F5.
For testing, I have the link and some cookie setting on the first page like this:
<?php
include("connect-db.php");
$id = "newuser";
setcookie("user_id", $id, time() + 31536000);
if (isset($_COOKIE["user_id"]))
echo "Welcome " . $_COOKIE["user_id"] . "!<br>";
else
echo "Welcome guest!<br>";
?>
<br>
<a href='next.php'>Next</a>
On the next page I have this php code:
<?php
header("Cache-Control: no-cache, must-revalidate, max-age=0"); // HTTP/1.1
header("Expires: Sat, 26 Jul 1997 05:00:00 GMT"); // Date in the past
header('Pragma: no-cache');
setcookie("user_id", "", time()-(60*60*24), "/");
echo "<br /> Welcome " . $_COOKIE["user_id"] . "!<br>";
?>
How can I make sure the cookie is unset on the second page, or the page is fetched anew?
This depends on what you are trying to do. If you have a dependency on the cookie on the client, then you may want to unset it using the client (JavaScript). If the dependency is on the server, then it'd be tough to know what your problem is since you can just write the second part of the code to not depend on the cookie after you "unset" it.
I don't fully recommend this as it's an extra page load that's probably not necessary, but you could also force the sending of headers.
if (isset($_COOKIE['user_id'])) {
setcookie(/* your params */);
header('Refresh: ' . $_SERVER['REQUEST_URI']);
exit;
}
you can try using the following code in second page
setcookie("user_id", "");
I hope you can solve your problem.

Why isn't PHP responding to my $_GET variables?

I have the following code in index.php (simplified):
<?php
print_r($_GET);
if ($_GET['f'] == "activate") {
//do stuff
}
if ($_GET['f'] == "disable") {
//do stuff
}
?>
and further down the page, some HTML:
<tr>
<td style='font-size: 0.9em;'>email#address.ac.uk</td>
<td style='font-size: 0.75em;'><span style="color: #00AF28;">Active</span></td><td style='font-size: 0.75em;'>Disable</td>
</tr>
The two hyperlinks will point the browser to the URL index.php?f=disable&id=email#address.ac.uk and index.php?f=disable&id=email#address.ac.uk, respectively.
Upon clicking the hyperlink (and directing the browser to index.php?f=disable&id=email#address.ac.uk), the result of print_r($_GET) is:
Array ( [f] => disable [id] => email#address.ac.uk )
However, the 'if' statement:
if ($_GET['f'] == "disable") {
//do stuff
}
Does not execute until I refresh the page, or press the enter key on the address bar.
I'm using Chrome 16 on OSX for this, with PHP5.
Many thanks
Try to disable the cache. You can do this by sending some http headers to the browser. Here are an example from php.net
<?php
header("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1
header("Expires: Sat, 26 Jul 1997 05:00:00 GMT"); // Date in the past
?>

Categories