How to check the user id on the page? id is in the database to which I made the connection.

Update

I need him to read the user id when moving to another page and allow him to perform any manipulations on the page. It goes to another page, and the user id can not see it so that it can do something there.

 if (isset($_GET['id'])) { $id =$_GET['id']; } else { exit("Вы зашил на страницу без параметра!"); } if (!preg_match("|^[\d]+$|", $id)) { exit("<p>Неверный формат запроса! Проверьте URL</p>"); } 

it gives me that I entered the page without a parameter, that is, I did not specify my id , but I need this to be done automatically. Sorry, I'm just just learning, and I still don't understand everything.

  • What exactly are you interested in? How to check if this id exists in the database? You can do this: SELECT id FROM table_name WHERE id = YOUR_ID Try to formulate your questions more precisely - BOPOH
  • Read about the session. About cookies, too, can read. - Visman
  • @Visman, thanks for the help)) through the sessions I fixed everything)) - artes
  • If possible, publish the solution found in response to your question . I am sure it will help many of your colleagues in the future. - Nicolas Chabanovsky

1 answer 1

Usually, the user ID is not passed through a GET parameter (address bar), where it can be easily forged. After authentication, by initiating a user session in which they store all the information necessary for checking access rights. Session data does not leave the server, and the client and server exchange the session identifier that the client usually stores in a cookie.

In order to use a session, it is sufficient to call the session_start() function at the beginning of the script. After that, you can store data in the $_SESSION superglobal array.

 <?php // Инициализируем сессию session_start(); ... if($id = is_auth(...)) { $_SESSION['id'] = $id; } 

After that, on any page where the session_start() function is called, you can use data from the $_SESSION (both for writing and for reading).

 <?php session_start(); ... if(isset($_SESSION['id'])) { ... }