Metodele GET si POST

postat acum 4 ani de Stefanescu Mihai in categorie PHP

In PHP exista 2 moduri de a transmite informatii intre browser si server.

  • Metoda GET
  • Metoda POST

Metoda GET

Metoda get trimite informatiile catre server codate in URL, separate de url prin semnul ?.

http://invata-programare.ro/index.php?nume1=valoare1&nume2=valoare2&nume3=valoare3

Informatiile trimise cu ajutorul acestei metode se pot recupera cu variabila globala $_GET (care este un array asociativ ce contine toate informatiile trimise prin GET)

  • Metoda GET produce un sir de caractere lung ce apare in logurile serverului si in casuta de adrese a browserului.
  • Metoda GET este restrictionata la 1024 caractere.
  • Nu folositi metoda GET pentru parole sau alte informatii importante.
  • Metoda GET nu poate trimite date binare (ex: imagini, documente, etc) catre server.
<?php
  if( $_GET["nume"] || $_GET["varsta"] )
  {
     echo "Salut ". $_GET['nume']. "<br />";
     echo "Ai ". $_GET['varsta']. " ani.";
     exit();
  }
?>
<html>
<body>
  <form action="<?php $_PHP_SELF ?>" method="GET">
  Nume: <input type="text" name="nume" />
  Varsta: <input type="text" name="varsta" />
  <input type="submit" />
  </form>
</body>
</html>

Metoda POST

Metoda POST transmite informatia prin intermediul headerelor HTTP. Informatia este encodata ca si in cazul metodei GET si pusa intr-un array asociativ numit $_POST.

Informatiile transmise prin metoda POST merg prin geaderele HTTP, deci securitatea informatiei depinde  de securitatea protocolului (Se poate folosi Secure HTTP pentru asigurarea securitatii).

  • In cazul metodei POST nu exista restrictii de marime
  • Metoda POST poate transmite informatii in ASCII cat si in binar
<?php
  if( $_POST["nume"] || $_POST["varsta"] )
  {
     echo "Salut ". $_POST['nume']. "<br />";
     echo "Ai ". $_POST['varsta']. " ani.";
     exit();
  }
?>
<html>
<body>
  <form action="<?php $_PHP_SELF ?>" method="POST">
  Nume: <input type="text" name="nume" />
  Varsta: <input type="text" name="varsta" />
  <input type="submit" />
  </form>
</body>
</html>

Variabila $_REQUEST

Variabila $_REQUEST contine informatiile din ambele metode explicate mai ($_GET, $_POST si $_COOKIE. Discutam despre $_COOKIE la lectia despre sesiuni si cookies).

<?php
  if( $_REQUEST["nume"] || $_REQUEST["varsta"] )
  {
     echo "Salut ". $_REQUEST['nume']. "<br />";
     echo "Ai ". $_REQUEST['varsta']. " ani.";
     exit();
  }
?>
<html>
<body>
  <form action="<?php $_PHP_SELF ?>" method="POST">
  Nume: <input type="text" name="nume" />
  Varsta: <input type="text" name="varsta" />
  <input type="submit" />
  </form>
</body>
</html>

 

Sunt un tanar programator din Bucuresti ce lucreaza in PHP/Mysql (MySqli/PDO), Laravel, CodeIgniter, MySQL, PostgreSQL, Wordpress, HTML5/CSS3, Sass, Photoshop si multe altele.
Google+ Community Facebook Group
Acest articol a fost mutat de pe vechea platforma.
Pentru orice eroare aparuta la mutare va rog sa ma contactati!

Comentariu postate de nike shoes la data de 15.05.2019
I and also my guys have already been viewing the best tips and tricks on your web page then quickly came up with a terrible suspicion I had not expressed respect to you for those tips. Those young men appeared to be stimulated to read them and already have honestly been tapping into them. Thanks for being considerably considerate and then for going for this sort of really good guides most people are really desperate to discover. My personal honest regret for not expressing gratitude to earlier.