PHP Programming for Beginners. Key Programming Concepts. How to use PHP with MySQL and Oracle databases (MySqli, PDO). Sergey D Skudaev. Читать онлайн. Newlib. NEWLIB.NET

Автор: Sergey D Skudaev
Издательство: Издательские решения
Серия:
Жанр произведения: Компьютеры: прочее
Год издания: 0
isbn: 9785449090157
Скачать книгу
<? echo $param2;?>”, 300, 200)”> </p>

      </form>

      </body>

      </html>

      The bold code must be one line without breaks. The <=> is used as a carry-over sign only.

      This is a script for a popup page:

      popup.php

      <!DOCTYPE html>

      <html lang=“en”>

      <html>

      <head>

      <title> Popup with params from parent window </title>

      <?

      if (isset ($_GET [‘param1”]))

      $param1 =$_GET [‘param1”];

      if (isset ($_GET [‘param2”]))

      $param2 =$_GET [‘param2”];

      ?>

      </head>

      <body>

      <?php

      print (” <p style=“align: center; font-size:40px; <=> color:#000;">”.

      $param1.” </p>”);

      print (’<p style=‘align: center; font-size:50px; <=>

      color: #000;">”. $param2.”</p>”);

      ?>

      </body>

      </html>

      Output: modeless window popup with text

      Hello There!

      Hi World!

      $_COOKIE & $_SESSION

      $_COOKIE []

      Let us try to use the $_COOKIE [] global variable.

      Edit form_methods.php by adding the following piece of code. The cookie code must be inserted before the HTML header. Otherwise you will get an error!

      The setcookie function will set the $pref variable to the $_COOKIE [] variable.

      <?

      $pref=“Mrs”;

      setcookie (“prefix”, $pref);

      ?>

      <html>

      <head>

      <title> Form Methods

      </title>

      </head>

      <body>

      <form method=“post” action="formoutputpage.php”>

      <p> <input type=“text” name=“greeting” size=“15”> </p>

      <p> <input type=“text” name=“name” size=“15”> </p>

      <p> <input type=“submit” name=“submit” value=“Salutation”> </p>

      </form>

      </body>

      </html>

      Edit the formoutputpage.php file like this:

      <?

      echo $_POST [‘greeting’];

      echo “”. $_COOKIE [‘prefix’];

      echo “”. $_POST [‘name’];

      ?>

      The output page displays:

      Hello Mrs. Emily.

      The setcookie function actually has many parameters; however, in this example, we will only use the following two: name and value.

      On the PHP.net website you can find the whole description of any PHP function, including setcookie.

      “bool setcookie (string $name [, string $value [, int $expire = 0 [, string $path [, string $domain [, bool $secure = false [, bool $httponly = false]]]]]])”

      If we had not used an expire parameter, our cookie would have expired when the browser closed. We did not use the path parameter, which means our cookie is accessible in the current directory only. For example, if your website page on which you set the cookie is in the root directory (public_html) and you have a subdirectory “public_html/upload” the cookie will not be accessible on the PHP page inside the upload directory. To make it accessible in all subdirectories, you should set path to "/" value. The path is the third parameter. If you use the third parameter you must also use the second parameter, “expire”. You’ll need to write the following:

      setcookie (“prefix”, $pref, 0, "/”);

      $_SESSION []

      Let us try to use the session variable. The session code must be inserted before the HTML

      header, otherwise you will get an error! Edit form_methods.php file:

      <?

      session_start ();

      $_SESSION [‘title’] =“Dr.”;

      setcookie (“prefix”, $pref, 0, "/”);

      ?>

      <html>

      <head>

      <title> Form Methods

      </title>

      </head>

      <body>

      <form method=“post” action="formoutputpage.php”>

      <p> <input type=“text” name=“greeting” size=“15”> </p>

      <p> <input type=“text” name=“name” size=“15”> </p>

      <p> <input type=“submit” name=“submit” value=“Salutation”> </p>

      </form>

      </body>

      </html>

      Edit formoutputpage.php file. The session_start () function starts the session. It must be used each time you assign value to the $_SESSION variable or read value from the $_SESSION variable.

      <?

      session_start ();

      echo $_POST [‘greeting’];

      echo “”. $_SESSION [‘title’];

      echo “”. $_COOKIE [‘prefix’];

      echo “”. $_POST [‘name’];

      ?>

      The output page will display:

      Hello Dr. Mrs. Emily

      You can transfer data from one page to another via link. Edit form_methods.php file. Data sent by link is transferred by the GET method. It can be read from the $_GET [] variable.

      With a link you can pass as many parameters as you want. The first parameter starts following a question mark character, after which an ampersand character is placed before each parameter as in the following example:

      page.php? param1=value1&param2=value2&param3=value3 and so on.

      HTTP protocol does not specify any maximum URL length. That would depend on the browser.

      <?

      session_start ();

      $_SESSION [‘title’] =“Dr.”;

      setcookie (“prefix”, “Mrs”);

      ?>

      <html>

      <head>

      <title>