This is more of a general browser related question than code checking. I have wrote a calculator function in php and everything works great in a traditional web browser on a pc, but for whatever reason my ipad will not display the calculated results.
I cannot share the all the code, but here's the bit w/ session data:
//Gather Form Data
session_start();
if(count($_POST) > 0) {
$_SESSION['dob-month'] = $_POST['dob-month'];
$_SESSION['dob-day'] = $_POST['dob-day'];
$_SESSION['dob-year'] = $_POST['dob-year'];
if(isset($_POST['submit'])){
$_SESSION['submit'] = 1;}
header("HTTP/1.1 303 See Other");
header("Location: " . $_SERVER['REQUEST_URI']);
die();
}
elseif (isset($_SESSION['dob-month'])||isset($_SESSION['dob-day'])||isset($_SESSION['dob-year'])){
$month = $_SESSION['dob-month'];
$day = ltrim(sanitizeNumInput($_SESSION['dob-day']),'0');
$year = sanitizeNumInput($_SESSION['dob-year']);
$submit = $_SESSION['submit'];
/*
Put database-affecting code here.
*/
session_unset();
session_destroy();
}
Is there any other reason other than a possible error in my code that would cause issues w/ the browser showing the results?
I'm not receiving any errors, there's just no output.
Related
I'm processing some data from external pages. The pages can be accessed sequentially, i.e http://localhost/info/1.html, http://localhost/info/2.html, http://localhost/info/3.html, ....
<?php
$id = 0;
$id = intval($_GET['id'])+1;
$url = 'http://localhost/info/'.$id.'.html';
if (!$html = #file_get_contents($url)) {
$url = 'http://localhost/crawler.php?id='.$id;
//header("Location: ".$url);
echo '<html><script>window.location.href = "'.$url.'";</script> </html>';
die();
}
//process data
$url = 'http://localhost/crawler.php?id='.$id;
//header("Location: ".$url);
echo '<html><script>window.location.href = "'.$url.'";</script></html>';
die();
?>
The problem is the PHP redirect stops showing error the page is not redirecting properly, and redirect using JavaScript looks like slower. Already tried using header with removing the echo.
**
Is there any way to speed up the process ?
**
If no $_POST errors are found the variables are set to sessions and the page is redirected to itself to clear the post array. Once the page is reloaded from the redirect the sessions are set to standard variables and the sessions are unset and destroyed.
Unfortunately I'm back where I started. Everything works great in chrome, but browsers like firefox and safari are not storing the sessions. Once redirected the page in those browsers does not output any results from the form submission and has an empty form.
So just to be clear some browsers are having trouble handling the header redirect and storing the session data, but I do not understand why.
header("Location: /poop-calculator/",TRUE,303);
Everything works in all browsers as intended without the header redirect above. But I need to get this working to avoid duplicate submissions on page refreshes.
Here's what I'm currently working with:
session_start();error_reporting(E_ALL); ini_set('display_errors',1);
if(count($_POST) > 0) {
//Error Checking
$EC = 0;
if (isset($_POST['submit'])&&empty($_POST['dob-month'])){
$EC++;
$errors[$EC] = "Select a month";
}
if (isset($_POST['submit'])&&empty($_POST['dob-day'])|| $_POST['dob-day'] < 1 || $_POST['dob-day'] > 31){
$EC++;
$errors[$EC] = "Enter a day between 1 and 31";
}
if (isset($_POST['submit'])&&empty($_POST['dob-year'])|| $_POST['dob-year'] < 1900 || $_POST['dob-year'] > date('Y')){
$EC++;
$errors[$EC] = "Enter a year between 1900 and ".date('Y');
}
if(!empty($errors)){
echo '<div class="woocommerce-error">
<div class="errorSpacer"></div>';
foreach($errors as $error){
echo '<div class="error-wrapper">
<div class="erroWrapperLeft">
<p class="errorIcon"></p>
</div>
<div class="erroWrapperRight">
<p class="errorText">'.$error.'</p>
</div>
</div>';
}
echo "</div>";
}
//set sessions
if(empty($errors)&& isset($_POST['submit'])){
(isset($_POST['dob-month'])? $_SESSION['dob-month'] = $_POST['dob-month']:$_SESSION['dob-month'] = null);
(isset($_POST['dob-day'])? $_SESSION['dob-day'] = ltrim(sanitizeNumInput($_POST['dob-day']),'0'):$_SESSION['dob-day'] = null);
(isset($_POST['dob-year'])?$_SESSION['dob-year'] = sanitizeNumInput($_POST['dob-year']):$_SESSION['dob-year'] =null);
(isset($_POST['submit'])?$_SESSION['submit'] = 1:$_SESSION['submit'] = null);
header("Location: /calculator/",TRUE,303);
}
//set variables and clear sessions
}
if (isset($_SESSION['dob-month'])||isset($_SESSION['dob-day'])||isset($_SESSION['dob-year'])||isset($_SESSION['submit'])){
(isset($_SESSION['dob-month'])? $month = $_SESSION['dob-month']:$month = null);
(isset($_SESSION['dob-day'])? $day = ltrim(sanitizeNumInput($_SESSION['dob-day']),'0'): $day = null);
(isset($_SESSION['dob-year'])?$year = sanitizeNumInput($_SESSION['dob-year']):$year=null);
(isset($_SESSION['submit'])?$submit = $_SESSION['submit']: $submit = null );
session_unset();
session_destroy();
}
How about removing the Header and doing this is javascript?
replace header("Location: /poop-calculator/",TRUE,303);
with echo '<script>window.location="/poop-calculator";</script>'
I've been working on this problem over the last couple days where a post redirect get function works in chrome, but mot other browsers like firefox and safari.
The code below should start a session, set $_POST variables as $_SESSION variables if they exist, assign the sessions to a variable, and then unset and destroy the sessions. Basically anything you pass through this test from should be passed through these variables and echoed out.
I'm trying to get this basic method to work so I can implement server side validation and avoid duplicate submissions with the 303 redirect. Please help I've been stuck for days and can't figure out why it only works in chrome.
<?php
session_start();
error_reporting(E_ALL); ini_set('display_errors',1);
if(count($_POST) > 0) {
$_SESSION['testFormText'] = $_POST['testFormText'];
if(isset($_POST['testSubmit'])){
$_SESSION['testSubmit'] = 1;}
header("HTTP/1.1 303 See Other");
header("Location: https://$_SERVER[HTTP_HOST]/test-form/");
die();
}
if (isset($_SESSION['testFormText'])){
$text = $_SESSION['testFormText'];
$submit = $_SESSION['testSubmit'];
/*
Put database-affecting code here.
*/
session_unset();
session_destroy();
}else{
$text = null;
$submit = null;
}
echo '<div class="testForm">
<form method="post" action="">
<input type="text" name="testFormText"/>
<input type="submit" name="testSubmit" value="submit">
</form>
</div>
';
if (!empty($submit)){
echo $text;
}
?>
UPDATE
I've tested different sections of the code using vardumps in an attempt to narrow down the problem, which I believe to be:
header("HTTP/1.1 303 See Other");
header("Location: https://$_SERVER[HTTP_HOST]/test-form/");
die();
From what I've read this is the correct method for a redirect to avoid duplicate submissions. Any idea what's wrong, or can someone please suggest an alternative?
I have added some server side validation to a dropdown box on one of my pages and when I did the page wouldn't work anymore. The code I added is as follows:
$show_form = true;
if (isset($_POST['submit'])) {
//All of the server side validations
$validator = new FormValidator();
$validator->addValidation("hospital_name","dontselect=000","No facility was chosen");
if ($validator->ValidateForm()) {
// All the variables from the submission form
$userid = $_SESSION['user_id'];
$hosp = $_POST['hospital_name'];
header('Location: ../site_hospital' . $hosp . '/hospital_submitform.php?usr=' . $userid . '&&hosp=' . $hosp);
exit;
$show_form = false;
} else {
echo "<B style='color:red;'>The following errors occurred:</B>";
$error_hash = $validator->GetErrors();
foreach ($error_hash as $inpname => $inp_err) {
echo "<p style='color:red;'>$inp_err</p>\n";
}
}}
if (true == $show_form) {
Through pure chance I added ob_start(); as part of my debugging to the beginning of the page and suddenly my code worked properly but I have no idea why and I was hoping the community could throw out an educated guess as to why. When the code stopped working it would not execute my header command above, the page would simply refresh and not change location, when I added ob_start(); to the top of the page the page redirected as planned. So the overall question is why would the page not direct using the header command without ob_start? I'm sure alot more detail and code is necessary for a definitive answer but I'm hoping someone has run into this before or has an educated guess that may lead me to my own answers. Thanks for any insight.
it's because you were writing to the output stream and preventing the header from working properly. once you started buffering other outputs, you removed the obstacle to the header's operation.
PROBLEM
I've got an admin panel. Currently only Mozilla is able to process log ins. Browsers like Chrome, IE, Opera won't even show any message carried through sessions thus no one is able to log in any browser but Mozilla.
SOME INFORMATION
I'm using PHP 5.3.6 on my server, PHP 5.3.5 on my local
computer.
My code is Object Oriented.
ini_set("session.use_only_cookies", 1); and
ini_set('session.cookie_secure', 1); are used in construction method
of my session class.
This website on SLL
Login process: First I gather all information from form, validate and gather data. After validation if everything is right, I send this data to login method in my session class.
public function login ($user) {
global $siteSettings;
if ($user) {
$this->id = $_SESSION['id'] = $user->id;
$this->username = $_SESSION['username'] = $user->username;
$this->fullName = $_SESSION['fullName'] = $user->fullName;
$this->group_id = $_SESSION['group_id'] = $user->group_id;
$this->groupName = $_SESSION['groupName'] = $user->groupName;
$this->lastLogin = $_SESSION['lastLogin'] = $user->lastLogin;
$this->isAdmin = $_SESSION['isAdmin'] = ($user->admin == 1) ? true : false;
$this->isAgent = $_SESSION['isAgent'] = ($user->agent == 1) ? true : false;
self::$language = $_SESSION['language'] = ($user->language != "" || $user->language != NULL) ? $user->language : self::$language;
if ($user->language != "" || $user->language != NULL) {
$_SESSION['language'] = $user->language;
}else {
if (!defined(DEFAULT_LANGUAGE)) {
$browserLang = "|".$_SERVER["HTTP_ACCEPT_LANGUAGE"];
$browserLang = getStringBetween($browserLang, "|","-", FALSE);
if (!file_exists(LANGUAGES.$browserLang.".php")) $browserLang = FALSE;
}
$_SESSION['language'] = ($browserLang) ? $browserLang : DEFAULT_LANGUAGE;
}
# When 2 Update session_id
$date = new DateTime("now");
$UpdateTime = $siteSettings->session->timeOut * 60;
$date->add(new DateInterval("PT".$UpdateTime."S"));
$_SESSION['SIDUpdateTime'] = $date->format("Y-m-d G:i:s");
# UPDATE LAST LOGIN & ADD SESSION ID
# Clear Fields
members::clearFields();
members::$fields['id'] = $_SESSION['id'];
members::$fields['lastLogin'] = date("Y.m.d G:i:s");
members::$fields['lastLoginIP'] = $_SERVER['REMOTE_ADDR'];
# GET THE SALT
$saltInfo = members::getData("id", "salt", members::$fields['id']);
# SETTING SESSION ID ENCRYPTION
crypt::setKey($saltInfo->salt);
members::$fields['sessionID'] = crypt::encode(session_id());
members::$fields['sessionIP'] = $_SERVER['REMOTE_ADDR'];
members::$fields['sessionAgent'] = $_SERVER['HTTP_USER_AGENT'];
members::save();
$this->loggedIn = true;
var_dump($_SESSION);
}
}
When I dumb the data I can see $_SESSION got some values.
Just to test it, I stopped the script where after var_dump($_SESSION); (added die();) I created test.php file and tried this;
<?php
ob_start();
session_start();
echo '<pre>';
var_dump($_SESSION);
echo '<pre>';
ob_end_flush();
?>
Output is array(0) {}
But when I try exactly the same thing with Mozilla, output of test.php is the way it should be (matching with login method's result in my session class).
I have tried from my local computer and I don't experience the same
problem.
I disabled all java script and jquery codes from the page just to
have no 'maybe' in my mind.
After dumping the data, script is stopped. That's why $_SESSION variable shouldn't change. For some reason when it is on the server only Mozilla is able to show expected result while other browsers shows NULL.
At this point I really don't know what to think of about this problem to try to solve it. All I can think of is, this problem is possibly related to server configuration. But then, PHP is server side programming. PHP shouldn't display different behavior for browsers like Jquery, CSS, HTML...
I'm sorry, I can't provide admin panel link. Considering this is an active admin panel. If necessary I could install it on another domain to let you try but I believe the information I gave above explains everything.
Thank you for your help in advance.
I had a similar problem... just enable the cookies.. so that after login the code to set the sessions will be executed and the sessions will be set. may be the sessions r not able to set...
also check this http://php.net/manual/en/function.session-cache-limiter.php
If something large doesn't work, trim it down, test & debug, and build up from there.
Does this work? (Run it twice).
<?php
session_start();
echo "Session ID: " . session_id() . "<br/>\n";
if (!isset($_SESSION['test']))
{
$_SESSION['test'] = "foobar";
echo "Setting session variable: ";
echo $_SESSION['test'];
}
else
{
echo "Restoring session variable: ";
echo $_SESSION['test'];
}
If this works in all browsers, it's got something to do with your code. An empty session might have something to do with a cookie that can't be written, for example. Also set error reporting to E_ALL | E_STRICT, so you'll see everything that goes wrong.
It turns out Mozilla FireFox is able to process some data but other browsers I tried with are not and therefore they reset the whole session with each page load.
I had no problem with my local computer but on the server I had sessions problem. I don't know why session_set_cookie_params(); and setcookie(); didn't work on the server so I had to code longer version;
private static function sessionLifeTime() {
global $siteSettings;
# HOW LONG WE WANT SESSIONS
$lifeTime = intval($siteSettings->session->timeOut) * 60;
if (isset($_SESSION['id']) && isset($_SESSION['lastActivity']) && (time() - $_SESSION['lastActivity'] > $lifeTime) ) {
// SEND INFORMATION TO USER
self::logout();
}
$_SESSION['lastActivity'] = time();
}
Replacing my method with the code above solved the problem.
Thank you all for your time, concern and interest.