Untitled

mail@pastecode.io avatar
unknown
php
2 years ago
3.9 kB
5
Indexable
Never
<?php
    session_start();
?>
<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title></title>
    </head>
    <body>
        <?php
        if (isset($_SESSION['views']))
            $_SESSION['views'] = $_SESSION['views'] + 1;
        else
            $_SESSION['views'] = 1;

        echo "Ova stranica je vidjena " .$_SESSION['views'] ." puta.";
        ?>
    </body>
</html>

<!------------------------------------------------------------------------------------------->
<!------------------------------------------------------------------------------------------->

<?php
    if (!isset($_SESSION['ime']) && !isset($_POST['ime'])) {
        // ako nema podataka, stampaj formu
        ?>
        <form action="<?php echo $_SERVER['PHP_SELF']?>" method="post">
            <input type="text" name="ime" />
            <input type="submit" name="submit" value="Unesite svoje ime" />
        </form>
        <?php
    }
    else if (!isset($_SESSION['ime']) && isset($_POST['ime'])) {
        if (trim($_POST['ime']) != "") {
            $_SESSION['ime'] = $_POST['ime'];
            $_SESSION['start'] = time();
            echo "Dobrodosli, " . $_POST['ime'] . ". Za Vas je aktivirana nova
            sesija. Kliknite <a href=" . $_SERVER['PHP_SELF'] . ">ovde</a> za REFRESH stranice.";
        }
        else {
            echo "GRESKA: Uneti Vase cenjeno ime!";
        }
    }
    else if (isset($_SESSION['ime'])) {
        echo "Dobrodosli nazad, " . $_SESSION['ime'] . ". Ova sesija
        aktivirana je " . round((time() - $_SESSION['start']) / 60) . " minut(a) ranije.
        Kliknuti <a href=" . $_SERVER['PHP_SELF'] . ">ovde</a> za REFRESH stranice.";
    }
?>


<!------------------------------------------------------------------------------------------->
<!------------------------------------------------------------------------------------------->


<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title></title>
</head>
<body>
<?php
    if (!isset($_COOKIE['posecen'])) {
        setcookie("posecen", "1", time() + 86400) or die("Cookie se ne moze setovati");
        echo "Ovo je prva Vasa poseta u toku danasnjeg dana.";
    }
    else {
        echo "Vec ste bili danas ovde!";
    }
?>
</body>
</html>




<?php
    if (!isset($_POST['email'])) {
        // ako forma nije submitovana
        // prikazi formu
        // a ako cookie vec postoji, popuni polje za email adresu
        ?>
        <html>
        <head></head>
        <body>
        <form action="<?php echo $_SERVER['PHP_SELF']?>" method="post">
            Unesite svoju e-mail adresu:
            <input type="text" name="email" value="<?php if (isset($_COOKIE['zadnja_submisija'])) echo $_COOKIE['email']; ?>">
            <input type="submit" name="submit" value="Posalji">
            <?php
            // takodje preracunaj vreme od poslednje submisije
            if (isset($_COOKIE['zadnja_submisija'])) {
                $dana = round((time() - $_COOKIE['zadnja_submisija']) / 86400);
                echo "<br /> $dana dan(a) od zadnje submisije";
            }
            ?>
        </form>
        <?php
    }
    else {
        // ako je forma submitovana
        // setuj cookies za email i timestamp
        // oba isticu za 30 dana
        if (trim($_POST['email']) != "") {
            setcookie("email", $_POST['email'], time() + (86400*30), "/");
            setcookie("zadnja_submisija", time(), time() + (86400*30), "/");
            echo "Vas email je sacuvan.";
        }
        else {
            echo "GRESKA: Unesite svoju email adresu!";
        }
        echo "<br><a href='index.php'>Pocetna</a>";
    }
?>

<!------------------------------------------------------------------------------------------->
<!------------------------------------------------------------------------------------------->