Why when performing via Ajax request the following code with session_start (); echo does not work and just do the script for the idea gives an error

php code:

session_start(); include "mysqlConnect.php"; $name = iconv("utf-8","windows-1251",$_POST['name']); $name = trim($name); $name = stripslashes($name); $name = htmlspecialchars ($name); $pass = iconv("utf-8","windows-1251",$_POST['pass']); $pass = trim($pass); $pass = stripslashes($pass); $pass = htmlspecialchars ($pass); $sql = mysqli_query ($db, "SELECT * FROM users WHERE firstname='".$name."' AND pass='".$pass."'"); $num_rows = mysqli_num_rows($sql); if ($num_rows > 0) { $arr = mysqli_fetch_assoc($sql); $_SESSION['id'] = $arr['id']; $_SESSION['firstname'] = $arr['firstname']; $_SESSION['lastname'] = $arr['lastname']; echo "0"; } else { echo "1"; } ?> 

JS code:

 /*Кнопка входа*/ var clickButtonLoginForm = $(".clickButtonLoginForm"); // Делаем выборку кнопки из DOM дерева и заносим её в переменную clickButtonLoginForm.click(function(){ var name_login = $("#name_login").val(); var pass_login = $("#pass_login").val(); if (name_login == "" || pass_login == "") { $("#error").text("Вы не заполнили все поля").removeClass("success").addClass("error").show().delay(3000).fadeOut(300); } else { $.ajax({ url: "action_login.php", type: "POST", data: {name: name_login, pass: pass_login}, success: function(data){ if (data) { if (data == 1) { alert("Такая пара логин/пароль НЕ найдена."); } else { if (data == 0) { alert("Вы успешно авторизовались. Обновите страницу (F5)."); } else { alert("Неизвестная ошибка"); } } } else { alert("Данные не получены!!!"); } }, error: function(){ alert("Неизвестная ошибка, возможно отсутсвует подлючение к интернету"); } }); } }); 
  • and what a mistake? - Jean-Claude
  • Warning: session_start (): Cannot send session cache limiter - headers already sent (output started at ** \ action_login.php: 1) in ** \ action_login.php on line 2 - TomasRedl
  • Why get a warning? - TomasRedl

1 answer 1

You have a markup somewhere before the session_start () line, and this is a title, so a warning is displayed. Transfer the session_start to the beginning of the output html markup. It is also possible that the space is added by encoding for unix systems, in this case, transcode your php file to utf 8 without bom, for example, via notepad ++

  • He is the very first - TomasRedl
  • added the answer - read the last sentence. - heff
  • Added @ all the rules - TomasRedl