Asked  2 Years ago    Answers:  5   Viewed   210 times

I appeared for php test, their I was asked one question for which I could not find the answer.

The question is like this.

echo "MESSI is injured!!";
header("Location:somepage.php");

Interviewer want both header and echo to be written on the same page.

I wonder how's it possible.It should give some error like

headers already sent by (output started at .....

Is it really possible to write echo and header onto same page !!!

 Answers

1

You can use Output Buffering as

ob_start();
echo "MESSI is injured!!";
header("Location:somepage.php");
ob_end_flush();

The problem is that we cannot send the header after we start sending the output. To solve this we buffer the output. The function ob_start turns output buffering on. While output buffering is active no output is sent from the script (other than headers), instead the output is stored in an internal buffer. So the echo output will be buffered. Next we send the header without any problem as we've not yet spit out any output. Finally we call ob_end_flush to flush the internal buffer contents and to stop output buffering.

Tuesday, November 29, 2022
1

Checking the engine source for headers_list and http_response_code, notice that the value for general headers and status code are separated:

// headers_list
SG(sapi_headers).headers

// http_response_code
SG(sapi_headers).http_response_code

But HTTP response code isn't the only header with dedicated storage: Content-Type does, too:

SG(sapi_headers).mimetype = NULL;

So what's going on here? The complete header() algorithm specifically checks for the following strings to adjust state:

  • HTTP/
  • Content-Type
  • Content-Length
  • Location
  • WWW-Authenticate

HTTP/ is checked specifically because that's how one set the status code explicitly before PHP 5.4: after that, http_response_code is available and is recommended for clarity. That header() was used is confusing, for the reason you're asking in this question and on general principle: the http header BNF clearly doesn't include status line:

header-field   = field-name ":" OWS field-value OWS

PHP handles the others separately because they are single-value headers and/or their value matters for efficiency in later calculations.

TL;DR: HTTP/ set by header() isn't included in headers_list() because HTTP/ status lines are not headers in the strict RFC sense. But for the PHP < 5.4 limitation that header() was the only way to set HTTP/ status, it'd likely have never been a confusing issue.

Friday, October 28, 2022
2

If you're running IIS with both PHP and ASP.NET installed, there's no reason why you shouldn't be able to run both within the same site.

Monday, December 26, 2022
 
mknjc
 
2

Do something like

header("Location: index.php?Message=" . urlencode($Message));

Then on index.php...

if (isset($_GET['Message'])) {
    print $_GET['Message'];
}

In other words, index.php will always check if it's being passed a message in the url. If there is one, display it. Then, just pass the message in the redirect

if you really want to use a modal popup, generate the js...

if (isset($_GET['Message'])) {
    print '<script type="text/javascript">alert("' . $_GET['Message'] . '");</script>';
}

Note that this will break if you use quotes in the message unless you escape them

Thursday, September 8, 2022
 
zahir
 
5

If you're trying to implement some kind of double buffering, you might want to take a look at this

I think your problem is that you need to create a bitmap, then attach the canvas to it, something like:

Bitmap bitmap = Bitmap.createBitmap(width, height, Config.RGB_565);
Canvas c = new Canvas(bitmap);

// then draw to the canvas..
// and when you're happy, draw the bitmap onto the canvas supplied to onDraw.

Just creating a canvas, doesn't make it appear on the screen.

You may also want to take a look at: this tutorial

If you have a surfaceView, then you can do something like this (don't have a compiler, but hopefully you get the gist):

SurfaceView view = (SurfaceView)findViewById(R.id.view);

SurfaceHolder holder = view.getHolder(); // save this where it can be accessed by your function

Canvas c 
try  {
    c = holder.lockCanvas();
    // draw stuff
}
finally {
    if(null != c) {
        holder.unlockCanvasAndPost(c);
    }
}
Thursday, December 1, 2022
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :
 

Browse Other Code Languages