PHP - examples - 07 : cookies, session, network functions

Revision:


cookies

The setcookie() function must appear BEFORE the <html> tag.

cookie named 'user'is not set!
code:
            <div>     
                <?php 
                    if(!isset($_COOKIE['$cookie_name'])){
                        echo "cookie named '" .$cookie_name."'is not set!";
                    } else{
                        echo "cookie named '".$cookie_name."' is set!<br>";
                        echo "value is: ".$_COOKIE['$cookie_name'];
                    }
                ?>
            </div>
        
cookies are disabled
code:
            <div>     
                <?php 
                    if(count($_COOKIE) > 0){
                        echo "cookes are enabled";
                    } else{
                        echo "cookies are disabled";
                    }
                   
                ?>
            </div>
        
code:
            <div>     
                <?php 
                    // Example
                    // Setting a cookie
                    setcookie("usertoken", "noice", time()+20*24*60*60);
                    // 20 days = 20*24*60*60 seconds

                    setcookie("usertoken", "", time()-3600)
                ?> 
            </div>
        

session

The session_start() function must be the very first thing in your document, before any HTML tags.

session variables are set.

favorite color is green.
favorite animal is dog.
Array ( [favcolor] => green [favanimal] => dog )
codes:
            <div>     
                <?php 
                    $_SESSION['favcolor'] = "green";
                    $_SESSION['favanimal'] = "dog";
                    echo "session variables are set.";
                    echo "<br><br>";

                    echo "favorite color is ".$_SESSION['favcolor'].".<br>";
                    echo "favorite animal is ".$_SESSION['favanimal'].".<br>";
                    print_r($_SESSION);
                ?>
            </div>
        
code:
            <div>     
                <?php 
                    session_unset();
                    session_destroy();
                ?>
            </div>
        

network functions

PHP has a set of built-in functions for working with network connections and protocols. These functions allow to create server scripts that can interact with other servers and clients over the network, such as sending and receiving data, handling connections, and performing network-related tasks.

passed

aliases: dns_check_record(),

bom12s01-in-f14.1e100.net

The http service is available
code:
            <div>     
                <?php 
                    $domain="lwitters.com";
                    if(checkdnsrr($domain, "MX")){
                        echo 'passed';
                    } else{
                        echo 'failed';
                    }
                ?>
                <p>aliases: dns_check_record(), </p>
                <?php 
                    $ip = "172.217.167.174";
                    echo gethostbyaddr($ip);
                    echo "<br><br>";
                
                    if(getservbyname('http','tcp')!==FALSE){
                        echo "The http service is available";
                    }else{
                        echo "The http service is not available";
                    }
                ?>