The user enters the data in the form, then the data goes to the handler if everything is ok, then redirected to the page with its profile. Nothing was displayed in the profile, checked with the Isset function on the $ login variable, false. In the handler -

<?php session_start(); // вся процедура работает на сессиях. Именно в ней хранятся данные пользователя, пока он находится на сайте. Очень важно запустить их в самом начале странички!!! if(isset($_POST['login'])) { $login = $_POST['login']; if($login == '') { unset($login); } } //заносим введенный пользователем логин в переменную $login, если он пустой, то уничтожаем переменную if(isset($_POST['password'])) { $password = $_POST['password']; if($password == '') { unset($password); } } //заносим введенный пользователем пароль в переменную $password, если он пустой, то уничтожаем переменную if(empty($login) or empty($password)) //если пользователь не ввел логин или пароль, то выдаем ошибку и останавливаем скрипт { exit("Вы ввели не всю информацию, вернитесь назад и заполните все поля!"); } //если логин и пароль введены,то обрабатываем их, чтобы теги и скрипты не работали, мало ли что люди могут ввести $login = stripslashes($login); $login = htmlspecialchars($login); $password = stripslashes($password); $password = htmlspecialchars($password); //удаляем лишние пробелы $login = trim($login); $password = trim($password); // подключаемся к базе include ("db.php"); // файл bd.php должен быть в той же папке, что и все остальные, если это не так, то просто измените путь $result = mysql_query("SELECT * FROM users WHERE login='$login'", $db); //извлекаем из базы все данные о пользователе с введенным логином $myrow = mysql_fetch_array($result); if(empty($myrow['password'])) { //если пользователя с введенным логином не существует exit("Извините, введённый вами login или пароль неверный."); } else { //если существует, то сверяем пароли if($myrow['password'] == $password) { //если пароли совпадают, то запускаем пользователю сессию! Можете его поздравить, он вошел! $login = $_SESSION['login'] = $myrow['login']; $id = $_SESSION['id'] = $myrow['id']; //эти данные очень часто используются, вот их и будет "носить с собой" вошедший пользователь header("Location: profile.php"); } else { //если пароли не сошлись exit("Извините, введённый вами login или пароль неверный."); } } ?> 
  • Called, at the beginning of the page - angers777
  • @ angers777, at the beginning of both pages? - xEdelweiss
  • Yes, in both - angers777
  • 2
    throw out courses popova! - johniek_comp
  • one
    @ angers777, you would bring more code, and you just have to guess: do you read the value from $ _SESSION, do you not do it exactly as in the handler, do you not read before the session starts, do you even have cookies and .P. - xEdelweiss

2 answers 2

Perhaps you did not call session_start() ?
This function just creates or restores the session. Without it, no parameters in $_SESSION will $_SESSION .

It also says that the session ID is transmitted via GET, POST or Cookies. Cookies disabled - no session.

Alternatively, it is worth testing it all without the influence of your other actions.

At the beginning of the handler, add:

 session_start(); $login = $_SESSION['login'] = 'MyLogin'; $id = $_SESSION['id'] = 123; echo '<pre>'; var_dump($login); var_dump($id); var_dump($_COOKIE); die; 

In profile (profile.php, I suppose):

 session_start(); $login = $_SESSION['login']; $id = $_SESSION['id']; echo '<pre>'; var_dump($login); var_dump($id); var_dump($_COOKIE); die; 

After that send the form and look at the output. Go to profile.php and again - output. Analyze .

  • Null Null - angers777
  • In the handler: string (7) "MyLogin" int (123) - angers777
  • Check for PHPSESSID cookies on the profile.php page. If you do not know how - see the new line in the answer: var_dump ($ _ COOKIE); - xEdelweiss
  • Thank you very much! Announced the session in the profile, it helped! =) - angers777
  • :) hmm .. but they said that they announced - xEdelweiss
 if(isset($_SESSION['login'])){ $login = $_SESSION['login']; } // и т.д.