PHP/Form/Query String

Материал из Web эксперт
Перейти к: навигация, поиск

parse_str() function parses string into various variables, setting the variables in the current scope.

 
The syntax is: void parse_str (string string)
<?
$url = "fname=wj&lname=gore&zip=43210";
parse_str($url);
?>



parse_str.php

 
<?php
   
   parse_str("http://www.wbex.ru?ln=name&zip=43210");
?>



Passing Complex Values in a Querystring

 
string serialize ( mixed value ) 
mixed unserialize ( string str ) 
<html> 
<?php
class someclass {
  protected $someval;
  public function setsomeval($newval) {
    $this->someval = $newval;
  }
  public function getsomeval() {
    return $this->someval;
  }
}
$myclass = new someclass ( );
$myclass->setsomeval ( "Hello World!" );
$myarray = array ();
$myarray [0] = "Hello";
$myarray = serialize ( $myarray );
$myarray = urlencode ( $myarray );
$myclass = serialize ( $myclass );
$myclass = urlencode ( $myclass );
?> 
</head>
<body>
<a
  href="index.html?passedarray=<?php
  echo $myarray;
  ?>. &amp;passedclass=<?php
echo $myclass;
?>">Output Current Value</a> 
<?php
if (isset ( $_GET ["passedclass"] ) && isset ( $_GET ["passedarray"] )) {
  
  $newclass = new someclass ( );
  $newclass = $_GET ["passedclass"];
  $newclass = stripslashes ( $newclass );
  $newclass = unserialize ( $newclass );
  echo "Object: " . $newclass->getsomeval () . "<br />";
  
  $newarray = array ();
  $newarray = $_GET ["passedarray"];
  $newarray = stripslashes ( $newarray );
  $newarray = unserialize ( $newarray );
  print_r ( $newarray );
}
?> 
</div>
</body>
</html>



Passing Numeric Values in a Querystring

 
<html>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
</head>
<body>
  <div align="center">
    <p>Click a link to change the text color of the verbiage below:</p>
    <a href="index.php?color=1">Green</a><br />
    <a href="index.php?color=2">Red</a><br />
    <a href="index.php?color=3">Blue</a><br />
    <a href="index.php">Reset</a>
    <?php
      if (isset ($_GET["color"])){
        $color = intval ($_GET["color"]);
      } else {
        $color = "";
      }
      if ($color == 1){
        $fontcolor = "00FF00";
      } elseif ($color == 2){
        $fontcolor = "FF0000";
      } elseif ($color == 3){
        $fontcolor = "0000FF";
      } else {
        $fontcolor = "000000";
      }
      ?><p style="color: #<?php echo $fontcolor; ?>; font-weight: bold;">Hello World!</p><?php
    ?>
  </div>
</body>
</html>



Passing String Values in a Querystring

 
<html>
</head>
<body>
  <div align="center">
    <p>Click a link to move to a new page:</p>
    <a href="index.php?page=content1.html">Content 1</a><br />
    <a href="index.php?page=content2.html">Content 2</a><br />
    <a href="index.php?page=content3.html">Content 3</a><br />
    <?php
      $page = trim (urldecode (stripslashes ($_GET["page"])));
      if (isset ($page) && $page != ""){
        if (is_file ($page)){
          require_once ($page);
        } else {
          echo "<p>Sorry, the page you have requested does not exist.</p>";
        }
      }
    ?>
  </div>
</body>
</html>