Website Development Prices

Search Blog

Wednesday, August 3, 2016

Preusmeravanje korisnika pomocu HTTP zaglavlja (Redirecting users using HTTP headers)

HTTP zaglavlje mozete napraviti pomocu funkcije header. Ako zelite da napravite zaglavlje za preusmeravanje  koje pretrazivacu pokazuje kuda treba da ode, pozovite funkciju header('Location: http://imevasegwebsajta.com/').

Primer: 

HTML kod

<!DOCTYPE html>
<html>
<head>
<title>Web forme i validacija unosa korisnika</title>
</head>



<body>

<h2>Preusmeravanje korisnika</h2>

<form name="forma" action="preusmeravanje-korisnika.php" method="post">
<input type="submit" value="preusmeri">
</form>

</body>

</html>

Sacuvajte ovaj fajl kao preusmeravanje-korisnika.html, u folderu htdocs, foldera xampp. U mom primeru, putanja je C:/xampp2/htdocs/PHPtuts/preusmeravanje-korisnika/preusmeravanje-korisnika.html.

Rezultat:




Napomena: header funkcija se mora pozvati pre nego sto se bilo koji rezultat posalje, na primer, HTML elementi, prazne linije u fajlu ili PHP-u. Takodje, ova funkcija sprecava (od verzije 5.1.2) da vise od jednog zaglavlja bude poslato odjednom kao zastita od injekcije napada na zaglavlje (en. header injection attacks).

PHP kod

<!DOCTYPE html>
<html>
<head>
<title>Web forme i validacija unosa korisnika</title>
</head>

<body>
<h1>Preusmeravanje korisnika pomocu HTTP zaglavlja</h1>

<?php
header('Location: http://www.suzukicycles.com/');
exit;
?>

</body>

</html>

Sacuvajte ovaj fajl kao preusmeravanje-korisnika.php u istom folderu kao i preusmeravanje-korisnika.html.

Primer 2: prikazacemo skup od tri dugmeta, koja korisniku omogucavaju da izabere jedno od prethodnih primera iz prethodnih clanaka. Napravicemo tri razlicita dugmeta, od kojih svaki ima svoju formu. Ime primera (html fajl) cemo proslediti kao atribut value dugmeta.

HTML kod

<!DOCTYPE html>
<html>
<head>
<title>Web forme i validacija unosa korisnika</title>

</head>
<body>

<h2>Preusmeravanje korisnika</h2>

<form name="forma1" action="preusmeravanje-korisnika2.php" method="post">
<input type="submit" name="dugme" value="php-dugmad-1">
</form>
<br />
<form name="forma2" action="preusmeravanje-korisnika2.php" method="post">
<input type="submit" name="dugme" value="php-polje-za-potvrdu">
</form>
<br />
<form name="forma3" action="preusmeravanje-korisnika2.php" method="post">
<input type="submit" name="dugme" value="phptekst">
</form>

</body>

</html>

Sacuvajte ovaj fajl kao preusmeravanje-korisnika2.html, u folderu htdocs, foldera xampp. U mom primeru, putanja je C:/xampp2/htdocs/PHPtuts/preusmeravanje-korisnika/preusmeravanje-korisnika2.html

U istom folderu treba da imate fajlove sa primerima iz prethodnih clanaka.



Rezultat:


Kada korisnik klikne neko od dugmadi, ime onog koje je izabrao se salje nazad do PHP skripta preusmeravanje-korisnika.php.

PHP kod

<!DOCTYPE html>
<html>
<head>
<title>Web forme i validacija unosa korisnika</title>
</head>

<body>
<h1>Preusmeravanje korisnika pomocu HTTP zaglavlja</h1>

<?php
$preusmeri = "Location: " . $_REQUEST['dugme'] . ".html";
echo header($preusmeri);
?>

</body>

</html>

Sacuvajte ovaj fajl kao preusmeravanje-korisnika2.php u istom folderu kao i preusmeravanje-korisnika2.html.

Rezultat:



HTTP header, you can make using the function header. If you want to make a header for redirection, which shows to search engine where to go, call the function header('Location: http://nameofyourwebsite.com/').

Example:

HTML code

<!DOCTYPE html>
<html>
<head>
<title>Web forms and validation of user input</title>

</head>
<body>

<h2>Redirecting user</h2>

<form name="form" action="redirecting-user.php" method="post">
<input type="submit" value="redirect">
</form>

</body>

</html>


Save this file as a redirecting-user.html, in the htdocs folder, of the folder xampp. In my example, the path is C:/xampp2/htdocs/PHPtuts/redirecting-user/redirecting-user.html.

Result:



Note: the header function must be called before any result is sent, for example, HTML elements, blank lines in a file or PHP. Also, this function prevents (from version 5.1.2) that more than one header is sent at once as a protection against header injection attacks.

PHP code

<!DOCTYPE html>
<html>
<head>
<title>Web forms and validation of user input</title>
</head>

<body>
<h1>Redirecting user using HTTP headers</h1>

<?php
header('Location: http://www.suzukicycles.com/');
exit;
?>

</body>

</html>

Save this file as redirecting-user.php in the same folder as the redirecting-user.html.

Example 2: we will displays a set of three buttons, which allow the user to choose one of the previous examples in previous articles. We will make three different buttons, each of which has its own form. Name of the example (html file) we will forward as an attribute value of a button.

HTML code

<!DOCTYPE html>
<html>
<head>
<title>Web forms and validation of user input</title>

</head>
<body>

<h2>Redirecting user</h2>

<form name="form1" action="redirecting-user2.php" method="post">
        <input type="submit" name="button" value="php-buttons-1">
</form>
<br />
<form name="form2" action="redirecting-user2.php" method="post">
<input type="submit" name="button" value="php-html-checkbox">
</form>
<br />
<form name="form3" action="redirecting-user2.php" method="post">
<input type="submit" name="button" value="sending-data">
</form>

</body>
</html>

Save this file as a redirecting-user2.html, in the htdocs folder, of the folder xampp. In my example, the path is C:/xampp2/htdocs/PHPtuts/redirecting-user/redirecting-user2.html.


In the same folder you need to have files with examples of previous articles.



Result:


When the user clicks one of the buttons, the name of the one he selected will be sent back to the PHP script redirect-user.php.

PHP code


<!DOCTYPE html>
<html>
<head>
<title>Web forms and validation of user input</title>
</head>

<body>
<h1>Redirecting user using HTTP headers</h1>

<?php
      $redirect_user = "Location: " . $_REQUEST['button'] . ".html";
      echo header($redirect_user);
?>

</body>
</html>


Save this file as redirecting-user2.php in the same folder as the redirecting-user2.html.

Result:



No comments:

Post a Comment

Note: Only a member of this blog may post a comment.