Passing parameters from one PHP to WEB SITE - php

I am setting a parameters in my PHP page and need to pass it to another WEB SITE, to execute operation on the WEB SITE (i don't need to get re result). I use header ("location : https://localhost/test=123") but its not executing it and display:
Warning: Cannot modify header information - headers already sent by (output started at /Applications/XAMPP/xamppfiles/htdocs/script1.php:33) in /Applications/XAMPP/xamppfiles/htdocs/script1.php on line 106
How can i simply just invoke it one time?
if there is solution from bash file,also can help...

The header function can only be used before ANY output is displayed on the screen.
For example:
<?php
echo( 'test' );
header('location: www.google.com');
?>
Would fail with that error you reported.

You already output content to use header().
I would recommend:
$sRemotePage = file_get_contents('https://localhost/test=123');
More here: http://php.net/manual/en/function.file-get-contents.php

You can use the HTTP meta element to set where the browser redirects the user. It needs to look like this:
<meta HTTP-EQUIV="Refresh" content="0; url=http://localhost/test=123">
Where 0 is the number of seconds the browser will wait before redirecting, and url is the redirected URL.
The other option would be using Output Buffering, but I am not entirely sure what you want to do and whether it can include ob_ or not.

Related

redirect to a page without Warning: Cannot modify header information - headers already sent by

so this is driving me crazy I understand the error. But I am trying to redirect to the page regardless. meaning : if condition = 1 => redirect to this page... or redirect to another.
any other alternatives?
Have you tried using <?php ob_start(); ?> at the start of the page?
You need to make sure nothing is being sent to the browser before you send the redirect header.
One possible solution (although quick and dirty) is using output buffering, so at the start of the page put
<?php
ob_start();
// any code you need to check whether to redirect
// note that output from between the ob_start()
// and the ob_end_clean() won't be sent to the user
ob_end_clean();
and then do your redirection checks and redirection. Remember, don't let anything output. So no echos, etc.

window.location (JS) vs header() (PHP) for redirection

using JS : (in <head> tag)
<script>window.location="https://stackoverflow.com";</script>
using PHP : (in <head> tag)
header('Location: https://stackoverflow.com');
end();
Which one I should use ? or another ?
and what about using <meta>?
<meta http-equiv="refresh" content="0;url=https://stackoverflow.com"/>
Many good answers , I don't know which answer I will accept, Thanks so much
The result is same for all options. Redirect.
<meta> in HTML:
Show content of your site, and next redirect user after a few (or 0) seconds.
Don't need JavaScript enabled.
Don't need PHP.
window.location in JS:
Javascript enabled needed.
Don't need PHP.
Show content of your site, and next redirect user after a few (or 0) seconds.
Redirect can be dependent on any conditions if (1 === 1) { window.location.href = 'http://example.com'; }.
header('Location:') in PHP:
Don't need JavaScript enabled.
PHP needed.
Redirect will be executed first, user never see what is after. header() must be the first command in php script, before output any other. If you try output some before header, will receive an Warning: Cannot modify header information - headers already sent
A better way to set the location in JS is via:
window.location.href = 'https://stackoverflow.com';
Whether to use PHP or JS to manage the redirection depends on what your code is doing and how. But if you're in a position to use PHP; that is, if you're going to be using PHP to send some JS code back to the browser that simply tells the browser to go somewhere else, then logic suggests that you should cut out the middle man and tell the browser directly via PHP.
It depends on how and when you want to redirect the user to another page.
If you want to instantly redirect a user to another page without him seeing anything of a site in between, you should use the PHP header redirect method.
If you have a Javascript and some action of the user has to result in him entering another page, that is when you should use window.location.
The meta tag refresh is often used on download sites whenever you see these "Your download should start automatically" messages. You can let the user load a page, wait for a certain amount of time, then redirect him (e.g. to a to-be-downloaded file) without Javascript.
PHP redirects are better if you can as with the JavaScript one you're causing the client to load the page before the redirect, whereas with the PHP one it sends the proper header.
However the PHP shouldn't go in the <head>, it should go before any output is sent to the client, as to do otherwise will cause errors.
Using <meta> tags have the same issue as Javascript in causing the initial page to load before doing the redirect. Server-side redirects are almost always better, if you can use them.
The first case will fail when JS is off. It's also a little bit slower since JS must be parsed first (DOM must be loaded). However JS is safer since the destination doesn't know the referer and your redirect might be tracked (referers aren't reliable in general yet this is something).
You can also use meta refresh tag. It also requires DOM to be loaded.
window.location.href = 'url';
is beter than
header('location:url');
because the header command is mustly return an error "Warning: Cannot modify header information - headers already sent"
using js window.location.href = 'url';
this is beter

header function [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Headers already sent by PHP
I have a PHP file which I'm using to check username and password. This part is working, but after successful login I would like to use header() to redirect to user panel page. This is the logged error that I'm getting:
[10-Dec-2012 12:25:26] PHP Warning: Cannot modify header information - headers already sent by (output started at /home2/jzperson/public_html/imes/php/login.php:10) in /home2/jzperson/public_html/imes/php/login.php on line 32
This is line 10:
<script src="http://code.jquery.com/jquery-1.7.2.min.js"></script>
And this is line 32:
header("Location: http://imes.jzpersonal.com/userpanel.html");
Any idea why?
You probably have some output echoed out before getting to the line 32 with your header call.
See description of the header function: http://php.net/manual/en/function.header.php
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.
Clarifications
To clarify things a little bit, the redirection using header() is performed by including a raw location response-header field to the server response. When the receiving party reads the response and sees that header field, it drops the current response and issues another request to the destination you provided.
Now, headers always come at the top (head) of the server response. That's why they are called headers! If you output any content, PHP will immediately "prefix" it with default headers and it's not possible to add any more of them after this point. So, by attempting to set another header later in your code, you get an error:
Cannot modify header information - headers already sent
By outputting HTML at line 10 you can no longer issue any more headers, because they were already sent (prefixed to your HTML output).
You can find more information about headers here: http://www.faqs.org/rfcs/rfc2616.html
Basically, you need to check whether the user is logged in or not (and redirect) before anything is sent to the browser (before HTML). Your code, then, would look something like this:
<?php
...
if($loggedIn)
{
header("Location: http://imes.jzpersonal.com/userpanel.html");
exit();
}
?>
<html>
...
<script src="http://code.jquery.com/jquery-1.7.2.min.js"></script>
You are trying to write something before header statement
Remove any echo statements/html content before header statement. That should do the trick
You could also cheat and just use output buffering - at the very beginning of the script tree use ob_start(); to begin capturing the output. You can use headers and set cookies etc as much as you like then.
At the last line of the script tree use ob_end_flush(); to send the output. You can also grab it to a variable to further process if you wish with $buffer = ob_get_clean();
Although its not a solution as such it does allow for a more flexible coding environment AND it will solve your above problem.
Its best to flush and die if you are going to be sending a Location header:
ob_start();
/* very long snip */
header('Location: somepage.php');
ob_end_flush();
die();
This will prevent any further processing after the location change has been sent.
Just as a side note: When I speak of a script tree I mean the include path - like put the ob_start(); into a header file thats included before anything else and a footer file that flushes (and processes if required) the output buffer. Remembering, as highlighted above, that Location changes should have the script halted immediately after.
Sessions may also need to be closed with a header Location followed by a die - to use that simply
ob_start();
/* very long snip */
header('Location: somepage.php');
ob_end_flush();
session_write_close();
die();
I found that one out after hours of wondering why session data was being lost! Bear that on mind.
You can't use header(); if anything has already been sent as output. This means HTML. Do all your PHP processing first, then output your HTML/JS.

Can't redirect to another page at the end of the PHP script

Bot of these lines:
echo '<br/><br/>'.$_SERVER["SCRIPT_NAME"]."?page=".$pager->GetVariableC."&threadID=".$threadID;
header("Location:".$_SERVER["SCRIPT_NAME"]."?page=".$pager->GetVariableC."&threadID=".$threadID);
Give me this:
/PoliticalForum/Thread/thread.php?page=2&threadID=6
Warning: Cannot modify header information - headers already sent by (output started at C:\xampp\htdocs\PoliticalForum\Thread\comments.php:42) in C:\xampp\htdocs\PoliticalForum\Thread\thread.php on line 348
How do I redirect at the end of the script?
When this error happens, you have already sent something to the browser, by using echo or by having a couple of new lines after the closing php tag. Be sure not to have any new lines or echoing something before redirecting.
You can't redirect after you've sent output to the client's browser using PHP's header().
What you can do is use a meta tag:
<meta http-equiv="refresh" content="2;url=http://www.destination.com/">
Where 2 is the time before the redirect in seconds, and the url is the destination. You can find more information about it here (you should read the drawbacks section).
You can't redirect after you've already sent any data to the browser, in this case the first line is doing so. Why are you trying to print something that you are never going to see (The browser would be immediately redirected)?
Either remove the outputting lines above the redirect if possible, or look into using output buffers if you can't modify the surrounding code.
If you want output and a redirection then you need to use a javascript redirection. http://www.tizag.com/javascriptT/javascriptredirect.php
What you're doing here is called an HTTP redirection (an HTTP 302), which is one of the HTTP headers sent to the browser as a response.
This error message is due to the fact that you echo'd content which has the effect of sending out all the buffered HTTP headers then tried to set a header (but it was already sent). To get a much better view of this I recommend all web developers install firebug and monitor the "Network" tab, you'll really get a better grasp on headers and their meanings.
Cheers

PHP redirect AFTER sending headers?

I have a function which needs to redirect the page if a variable is set etc...
The problem is, this function is at the bottom of the php page.
This means, I have outputted alot of information, so I get a headers warning.
"Warning - Headers already sent by ..."
Is there any way to redirect after headers are sent?
Thanks
What you should do is put ob_start() at the very beginning of your page, and ob_flush() at the very end. This way you don't run into headers already sent errors.
See those functions for further reference.
There are ways, but they're basically workarounds:
The simplest one is the meta http-equiv:
<meta http-equiv="Refresh" content="0;http://www.example.com/newlocation">
some browsers will not like this when it's outside of the <head> element, and drop into quirks mode
Or, you can try the JavaScript redirect:
<script>
window.location = 'http://www.example.com/newlocation';
</script>
which obviously won't work without JavaScript.

Categories