Header location is not working on live server - javascript

I Have one registration and payment page. program flow is like registration > confirmation > payment. After validation and calculation in registration page need to go to confirmation page.
My code is
<?php
ob_start();
session_start();
include("header.php");
include("ini.php");
date_default_timezone_set('Asia/Brunei');
header('Cache-Control: max-age=900');
if (isset($_POST['submit'])) {
$error = 0;
//validations
if ($error <= 0) {
//do some calculation
header('location:confirm.php');
}
}
<?php
ob_flush();
?>
Control entered in to if ($error <= 0) clause, but stay on the registration page.
I tried many ways like
header('location:confirm.php', true, 302);
instead of
header('location:confirm.php');
removed
ob_start() and ob_flush()
add exit() after header()
then it goes to blank page.
Also try to use
echo '<script type="text/javascript">';
echo 'window.location.href="confirm.php";';
echo '</script>';
Then the control goes to confirmation page but url seems to be as registration.php
But header('location:confirm.php'); is working fine in my local server. Anybody please help me to resolve this issue.. Is there any alternate way for header(). Looking forward to you guys.. Thanks

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.
Also try to change header('Location:confirm.php');
(Note L is capital since its work in your local server may be problem with strict php type try this once)

$host = $_SERVER['HTTP_HOST'];
$uri = rtrim(dirname($_SERVER['PHP_SELF']), '/\\');
$extra = 'confirm.php';
header("Location: http://$host$uri/$extra");

use ob_end_flush() at the end. This should work like charm.

i had the same issues for years but today i discovered that u don't have to have any html or blank space before the header word .Try it out

Related

PHP refreshing using jQuery doesn't work in IE

I wanted to create a function in javascript/jQuery, that uses php script to refresh some data on my website. I'm using it like this:
function aval()
{
...
$("#dost").load("readadmin.php?date="+dzien+"&diff="+diff+"&dtyg="+d);
setTimeout(aval,200);
}
where #dost refers to some div at the end of my site
Then, my readadmin.php file looks like this:
<?php
$diff=$_GET["diff"];
$name = $_GET["date"];
$dtyg = $_GET["dtyg"];
$plik = fopen("myfile","r");
$tekst = fread($plik,filesize("myfile"));
fclose($plik);
$bookings = explode(";;;;", $tekst);
for ($i=0; $i<(sizeof($bookings)-1); $i++)
$data[$i] = explode(":::",$bookings[$i]);
echo "<script>";
// here I'm echoing some javascript code, that basically changes styles on my page,
//depending on my variables and data from a file
echo "</script>";
?>
Also, on my site (admin.php) I use login, so my site looks like this:
<?php
session_start();
if(!session_is_registered('pass'))
header('location: login.php');
?>
...html code
login.php:
..something not important, forms to get $_POSTs etc..
<?php
if ($_POST["login"] == $login && $_POST["pass"] == $pass)
{
session_register('login');
session_register('pass');
header("location: admin.php");
}
?>
Now, everything works great on Chrome or Firefox, but not on IE. When I change data in my file, IE doesn't react to this, and keep displaying old data. When I put session_start() and session_destroy() at the begining and end of readadmin.php, then data is refreshing properly, but whatever action I do, it logs me out. Nothing strange here, I start a new session every 200ms so I get kicked.
One more thing: when I'm changing data that goes to readadmin.php from site, it displays proper data back, and then immediately changes to old data.
Please help.
You can add timestamp to url so the browser will not cache the file:
$("#dost").load("readadmin.php?date="+dzien+"&diff="+diff+"&dtyg="+d+"&t="+Date.now());

Appending parameter to existing URL cause infinite loop

I have a page index.php, I want to append a parameter to the end of URL upon a certain action by user. So the result would be index.php?param=1
Using the below PHP code causing an infinite redirect loop.
<?php
header("Location: index.php?param=1");
?>
Using the below Javascript code result in infinite refresh loop.
window.location = "?param=1";
How can I append that parameter only once without encountering any loop using either PHP, Javascript or jQuery?
For the PHP version, try:
<?php
if( !isset( $_GET['param'] ) ){
header('location: index.php?param=1' );
}
?>
Please try add host name and then try.
Example - If you are using localhost.
<?php
header("Location: http://localhost/index.php?param=1");
?>
Apart from using Apache's mod_rewrite, you could check if the additional parameter is already set, e.g.
<?php
if (isset($_SERVER['QUERY_STRING']))
header("Location: index.php?param=1");
?>
It is better to use .htaccess files to set redirects due to there is the good practice to store logic and server configuration separately.
Here is .htaccess code for you:
Redirect /path/to/index.php http://yourdomain.com/path/to/index.php?param=1
or if you need to have permanent redirect use
Redirect 301 /path/to/index.php http://yourdomain.com/path/to/index.php?param=1
If you do not use Apache but Nginx or something else check their documentation how to make redirects. Typically it is very easy.

PHP redirect header is not redirecting?

I am using following code to redirect
header('Location: '.$fileID.'php');
}
else if (!isset($_SESSION['login']))
echo '<p>you need to register first.</p>' ;
?>
$fileID has a value bacuse I am using this value in other part of code and that works fine. I also tried
echo '<script>window.location="'.$fileID.'php"</script>';
but got a notfound error. I would like to get it working by any of these methods but would prefer the header method. Any help is appreciated.
I wanted to put this in a comment but I don't have enough reputation :( Anyways try changing your header from this:
header('Location: '.$fileID.'php');
To this:
header('Location: '.$fileID.'.php');
You should exit; after redirect with headers, because the script will go on executing otherwise which can lead to unintended side effects.
but got a notfound error.
URL seems wrong, echo the output and see if it matches your expected location.

How can you embed PHP within a Javascript function?

I am trying to add a CAPTCHA option to a specific page on a website. The validation code for the page is written in Javascript, and the PHP CAPTCHA documentation (http://www.phpcaptcha.org/documentation/quickstart-guide/) is given strictly in PHP. I've managed to add the CAPTCHA box to the page and everything else up until where the code verifies the user's CAPTCHA entry. I am having trouble merging the two because:
a) they are written in different languages
b) my knowledge in PHP/Javascript is very basic
Here is a snippet of the page's original validation code:
function validate(formData, jqForm, options){
// # Valid?
valid = false;
// # Validate Contact Form
var errs = new Array();
// # Contact Name
if($('#ContactName').val() == ''){
errs.push(['#ContactName', 'Enter your name.']);
} else if(label = $('#ContactNameLabel label.error-message')){
label.fadeOut();
}
I want to repeat the same process except with the user's CAPTCHA entry. The following code is given in the PHP CAPTCHA documentation:
include_once $_SERVER['DOCUMENT_ROOT'] . '/securimage/securimage.php';
$securimage = new Securimage();
and
if ($securimage->check($_POST['captcha_code']) == false) {
// the code was incorrect
// you should handle the error so that the form processor doesn't continue
// or you can use the following code if there is no validation or you do not know how
echo "The security code entered was incorrect.<br /><br />";
echo "Please go <a href='javascript:history.go(-1)'>back</a> and try again.";
exit;
}
The code can be found, along with instructions, in the link given above. My question is: how can I implement the given PHP code inside the Javascript function that I have? To my understanding, it is possible to embed PHP inside Javascript as long as the forum is written using PHP (and I can confirm that the website I'm working on is built using CakePHP) - I am just lost with the syntax/how to go about executing this (if it is possible).
If anyone could offer me a helping hand that would be greatly appreciated! Thank you in advance.
You can write PHP within JavaScript if the JavaScript is in the View (as opposed to being in a .js file)
Example:
<?php
$message = "Hello World";
?>
<script>
alert("<?php echo $message; ?>");
</script>
You can't do that as PHP is a server-side language and Javascript is a client-side one. By the time your browser sees the page, all the PHP processing has finished on the server and all that's left is client-side rendering of the page. You will need to validate the CAPTCHA on the server-side.

Pass parameters to location.href in javascript instead if HEADER()

I am new to wordpress.
I have tried the following code
header("Location: $location", true, $status);
I want to redirect the page to particular page and i am getting this kind of error
"Cannot modify header information - headers already sent by (output started at /home/content/c/l/a/clareb23/html/client4/wp-content/plugins/featured-articles-lite/main.php:773) in /home/content/c/l/a/clareb23/html/client4/wp-includes/pluggable.php on line 870"
Any suggestions would be appreciated.
Based on comment
You can do it like this:
<script>
window.location = '<?php echo $location?>?myvar=true&status=<?php echo $status?>';
</script>
Make sure that you do not echo/print anything on screen before header(...) statement and that there is no whitespace before opening php tag eg <?php. It is also good practice to put exit() after header() function.

Categories

Resources