0

I have a webpage which calls a php file to create cookies on the user's machine after he clicks the Submit button. But as of now, in the browser, my php file opens up. Is it possible that I can redirect control to my php file, but it does not loads up in browser and somehow performs its actions in backend. If yes, please tell me briefly how?

HTML:

<html>
    <body>
        <form method="post" action="template.php">

           <h1>Online Profile</h1>
            Some text goes in here

           <p style="text-align: center;"> 
               <button name="optout" id="optout" type="submit"> Opt-Out </button>
           /p> 
       </form>
    </body>
</html>

PHP:

<?php

if( isset($_POST['optout']))
{
    echo("Hello World!");

    #If user presses the opt-out button, create a cookie on user's system.
    $cookie_name = "test";
    $cookie_value = "Katie";
    $number_of_days = 365 ;
    $date_of_expiry = time() + 60 * 60 * 24 * number_of_days;

        #for web domain:
    #setcookie( $cookie_name, $cookie_value, $date_of_expiry, "/", "example.com");  

    #for localhost:        
    setcookie( $cookie_name, $cookie_value, false, "/", false);

    #If cookie is already created, then display its contents; otherwise print cookie not set message.
    if(!isset($_COOKIE[$cookie_name])) 
    {
        echo "Cookie named '" . $cookie_name . "' is not set!";
    } 
    else 
    {
        echo "Cookie '" . $cookie_name . "' is set!<br>";
        echo "Value is: " . $_COOKIE[$cookie_name];
    }
}
Barney
  • 2,355
  • 3
  • 22
  • 37
Ambidextrous
  • 882
  • 2
  • 12
  • 26

0 Answers0