Should I set Content-Type header twice in PHP?

user4483652

I have a PHP script that inserts data into MySQL which has to be done using UTF-8.
If the action was successfully executed, I would like to echo "success" using JSON which is why I need to set the Content-Type header twice or am I wrong?

Example code:

<?php
header('Content-Type: text/html; charset=utf-8'); // header one
$sql = "INSERT INTO MyGuests (firstname, lastname, email)
VALUES ('Dennis', 'Enström', '[email protected]')";
if (mysqli_query($conn, $sql)) {
header('Content-Type: application/json'); // header two
echo json_encode('success');
}

Is this the correct way to do it or how should I do it?

UPDATE
The MySQL values comes from $_POST and the HTML document has UTF-8 set.

Julie Pelletier

Use mysqli_set_charset() (PHP manual page) to set the charset used by the Mysql client interface.

The use of header() sends a header to the client's browser saying how to read your JSON encoded "success" string.

It may be a good idea at this point to validate the encoding selected in your database.

Note that your code is currently missing the command to open the mysqli connection before running the query.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

jQuery: Content type header is not set

From Dev

Set Content-Type header

From Java

When should I use HTTP header "X-Content-Type-Options: nosniff"

From Dev

Can't set Content-Type header

From Dev

Content-Type header not getting set in Tornado

From Dev

How to set the Content-Type header for a RestRequest?

From Dev

Set default content type header of Spring RestTemplate

From Dev

How to set the Content-Type header for a RestRequest?

From Dev

Content Type Character Set in PHP Header of JSON Data for utf8_unicode_ci - Ajax Call

From Dev

Get current Content-Type header in PHP

From Dev

Get PHP content-type from header()

From Dev

What content type should be in http header of soap 1.2 message?

From Dev

Should Content-Type header be present when the message body is empty?

From Dev

SVG sprite file - is it necessary for it to have ".svg" extension, or is php/nothing ok so long as the content type header is set correctly?

From Dev

How to set Content-Type header for JMS message

From Java

How do you set the Content-Type header for an HttpClient request?

From Dev

Set the header( content-type: image/<ANY IMG FORMAT>)

From Dev

How to set Content-Type header on Ring-Compojure application

From Dev

Content-Type header not being set with Angular $http

From Dev

Is it possible to manually set the content-type header in reply.file()?

From Dev

Set content-type header to json for request.post

From Dev

PHP cURL Content-Type is not set

From Dev

How do I set the content type in Grails?

From Dev

PHP mail displaying content type header inside the body

From Dev

how to write header content type json data to csv file in php?

From Dev

How can I access the "Content-Type" header of an NSHTTPURLResponse?

From Dev

How do i specify spray Content-Type response header?

From Dev

How do i specify spray Content-Type response header?

From Dev

Which separator should be used in the Content-Type header for a multipart data request? Comma or Semicolon?

Related Related

  1. 1

    jQuery: Content type header is not set

  2. 2

    Set Content-Type header

  3. 3

    When should I use HTTP header "X-Content-Type-Options: nosniff"

  4. 4

    Can't set Content-Type header

  5. 5

    Content-Type header not getting set in Tornado

  6. 6

    How to set the Content-Type header for a RestRequest?

  7. 7

    Set default content type header of Spring RestTemplate

  8. 8

    How to set the Content-Type header for a RestRequest?

  9. 9

    Content Type Character Set in PHP Header of JSON Data for utf8_unicode_ci - Ajax Call

  10. 10

    Get current Content-Type header in PHP

  11. 11

    Get PHP content-type from header()

  12. 12

    What content type should be in http header of soap 1.2 message?

  13. 13

    Should Content-Type header be present when the message body is empty?

  14. 14

    SVG sprite file - is it necessary for it to have ".svg" extension, or is php/nothing ok so long as the content type header is set correctly?

  15. 15

    How to set Content-Type header for JMS message

  16. 16

    How do you set the Content-Type header for an HttpClient request?

  17. 17

    Set the header( content-type: image/<ANY IMG FORMAT>)

  18. 18

    How to set Content-Type header on Ring-Compojure application

  19. 19

    Content-Type header not being set with Angular $http

  20. 20

    Is it possible to manually set the content-type header in reply.file()?

  21. 21

    Set content-type header to json for request.post

  22. 22

    PHP cURL Content-Type is not set

  23. 23

    How do I set the content type in Grails?

  24. 24

    PHP mail displaying content type header inside the body

  25. 25

    how to write header content type json data to csv file in php?

  26. 26

    How can I access the "Content-Type" header of an NSHTTPURLResponse?

  27. 27

    How do i specify spray Content-Type response header?

  28. 28

    How do i specify spray Content-Type response header?

  29. 29

    Which separator should be used in the Content-Type header for a multipart data request? Comma or Semicolon?

HotTag

Archive