Page 1 of 2 12 LastLast
Results 1 to 10 of 24

Thread: Day 10 - Let's Eat Cookie - Become PHP Expert in 30 days

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Nov 2009
    Location
    kerala
    Posts
    19,076

    Default

    Do we need to use _ ( Underscore ) if our variable have three words ?
    php_expert_name

    Before we used like

    secretNumber
    userNumber
    cowsCanFly

    Any difference between these two variable type?

  2. #2
    Join Date
    Sep 2003
    Posts
    3,040

    Default

    Quote Originally Posted by sibichan1 View Post
    Do we need to use _ ( Underscore ) if our variable have three words ?
    php_expert_name

    Before we used like

    secretNumber
    userNumber
    cowsCanFly

    Any difference between these two variable type?

    It is better we follow one type of coding style. So far PHP had no coding guidelines like many other programming languages. So we used whatever we like, but when working on a team, it is better we follow same coding conventions. In vShare, we use $video_info, $user_name etc.. At that time no such coding guidelines where present and we follow that rule in out code.

    Now PHP have a coding style

    http://www.php-fig.org/psr/psr-2/

    This coding style is supported by many large projects as you can see on home page of that site

    http://www.php-fig.org/

    Laravel, Zend Framework 2, Symfony2, Joomla, Drupal, eZ Publish, phpBB, Doctrine, CakePHP, Composer and many more. This list include popular PHP frameworks like Zend, Symfony and Laravel.

    But we can't change coding style of exisiting project in one day, there are lot of code to change, so it may take time, for newer code, it is better follow a coding style, so everyone do the same, not what each programmer like, this may it easy to read/understand code.

    In the code, i used

    Code:
    $_COOKIE['php_expert_name']
    It is better follow the PHP variable naming convention and use

    Code:
    $_COOKIE['phpExpertName']
    I will update the script to avoid further confusion.
    Become PHP Expert in 30 days
    FreeMarriage.com - Free Online Matrimonial
    FlashWebHost.com - Professional Web Hosting, Designing.

  3. #3
    Join Date
    Nov 2004
    Location
    India
    Posts
    65

    Default

    http://php.flashwebhost.com/stefin/set_cookie.php

    Code:
    <?php
    
    
    if (isset($_COOKIE['flipkartCustomer'])) {
        echo $_COOKIE['flipkartCustomer'] . ',Welcome to flipkart, SPECIAL OFFER- 50% OFF ON ALL GAMES';
    exit;
    }
    
    
    if (isset($_POST['flipkartCustomer']) && strlen($_POST['flipkartCustomer']) > 5) {
       setcookie('flipkartCustomer', $_POST['flipkartCustomer'], time()+3600);
       echo 'COOKIE Set. Close the browser. Revisit this page, I will remember your name';
    } else {
    
    
    echo'
        Enter your name ?
        <form method="post" action="">
        <input type="text" name="flipkartCustomer">
        <button type="submit" name="whatever">Enter Name</button>
      ';
    }

  4. #4
    Join Date
    Sep 2003
    Posts
    3,040

    Default

    Quote Originally Posted by stefin View Post
    http://php.flashwebhost.com/stefin/set_cookie.php

    Code:
    <?php
    
    
    if (isset($_COOKIE['flipkartCustomer'])) {
        echo $_COOKIE['flipkartCustomer'] . ',Welcome to flipkart, SPECIAL OFFER- 50% OFF ON ALL GAMES';
    exit;
    }
    
    
    if (isset($_POST['flipkartCustomer']) && strlen($_POST['flipkartCustomer']) > 5) {
       setcookie('flipkartCustomer', $_POST['flipkartCustomer'], time()+3600);
       echo 'COOKIE Set. Close the browser. Revisit this page, I will remember your name';
    } else {
    
    
    echo'
        Enter your name ?
        <form method="post" action="">
        <input type="text" name="flipkartCustomer">
        <button type="submit" name="whatever">Enter Name</button>
      ';
    }
    Thank you for the discount.
    Become PHP Expert in 30 days
    FreeMarriage.com - Free Online Matrimonial
    FlashWebHost.com - Professional Web Hosting, Designing.

  5. #5
    Join Date
    Nov 2009
    Location
    kerala
    Posts
    19,076

    Default PHP setcookie and delete cookie

    http://php.flashwebhost.com/sibichan...set_cookie.php

    PHP Code:
    <?php

    if (isset($_COOKIE['memberName'])) {

    echo 
    'Hi ! 'strtoupper($_COOKIE['memberName']) .', Welcome to Group.<br>(<a href=delete_cookie.php>Click here to delete cookie</a>)';

    exit;

    }
    if (isset(
    $_POST['memberName']) && strlen($_POST['memberName']) > 3) {

        
    setcookie('memberName'$_POST['memberName'], time()+3600);

        echo 
    'Cookie Set. Close the browser. Revisit this page, I will remember your name <br>(<a href=delete_cookie.php>Click here to delete cookie</a>)';

    }else {
    echo 
    '    Enter your Name    <form method="post" action="">    <input type="text" name="memberName">    <button type="submit" name="whatever">Enter Group</button>    </form>

    '
    ;

    }

  6. #6
    Join Date
    Sep 2003
    Posts
    3,040

    Default

    Quote Originally Posted by sibichan1 View Post
    http://php.flashwebhost.com/sibichan...set_cookie.php

    PHP Code:
    <?php

    if (isset($_COOKIE['memberName'])) {

    echo 
    'Hi ! 'strtoupper($_COOKIE['memberName']) .', Welcome to Group.<br>(<a href=delete_cookie.php>Click here to delete cookie</a>)';

    exit;

    }
    if (isset(
    $_POST['memberName']) && strlen($_POST['memberName']) > 3) {

        
    setcookie('memberName'$_POST['memberName'], time()+3600);

        echo 
    'Cookie Set. Close the browser. Revisit this page, I will remember your name <br>(<a href=delete_cookie.php>Click here to delete cookie</a>)';

    }else {
    echo 
    '    Enter your Name    <form method="post" action="">    <input type="text" name="memberName">    <button type="submit" name="whatever">Enter Group</button>    </form>

    '
    ;

    }
    Good use of delete cookie.
    Become PHP Expert in 30 days
    FreeMarriage.com - Free Online Matrimonial
    FlashWebHost.com - Professional Web Hosting, Designing.

  7. #7
    Join Date
    May 2014
    Posts
    21

    Default

    http://php.flashwebhost.com/austin/day10.php
    Code:
    <?php
      if(isset($_COOKIE['name']))  {
       echo 'Hello '.$_COOKIE['name'].' welcome to my world';
         exit; }
       if (isset($_POST['name'])) {
       setcookie('name',$_POST['name'],time()+(60*3));
       echo 'your cookie is set';
       }
       else {
       echo 'Whats your name?
       <form method="POST" action="">
       <input type="text" name="name">
       <button type="submit">GO</button>
       </form> ';
    }

  8. #8
    Join Date
    Apr 2005
    Posts
    46,704

    Default

    http://php.flashwebhost.com/mini/cookie.php

    Code:
    <?php
    if (isset($_COOKIE['Yoga_name'])) {
    echo $_COOKIE['Yoga_name'] . ', Welcome to Free Online Yoga Videos';
    exit;
    }
    
    if (isset($_POST['Yoga_name']) && strlen($_POST['Yoga_name']) > 3) {
    setcookie('Yoga_name',$_POST['Yoga_name'], time() + 7200);
    echo 'Cookie Set. Close the browser. Revisit this page, I will remember your name';
    } else {
     echo '
            What is your name ?
            <form method="post" action="">
            <input type="text" name="Yoga_name">
            <button type="submit" name="submit">Enter Web Site</button>
            </form>
        ';
    }

  9. #9
    Join Date
    Oct 2003
    Location
    Kochi, Kerala, India
    Posts
    21,389

    Default

    Can somebody explain this portion of the script ?

    Code:
    setcookie('phpExpertName', $_POST['phpExpertName'], time()+3600);
    Why phpExpertName and $_POST['phpExpertName'] mentioned together ?

  10. #10
    Join Date
    Oct 2003
    Location
    Kochi, Kerala, India
    Posts
    21,389

    Default

    I do understand that it is the syntax of cookie. Hope I am right.

    http://php.flashwebhost.com/vineesh/...set_cookie.php

    PHP Code:
    <?
    if(isset($_COOKIE['userName'])) {
    echo '<h2 style="color:red">Hi ' . $_COOKIE[userName] . '!, welcome back.</h2>';
    exit;
    }


    if(isset($_POST['userName']) && strlen($_POST['userName']) > 3) {
    setcookie('userName', $_POST['userName'], time() + 3600);
    echo '<h3 style="color:red">You may close the browser and re-login, I will remember your Name. </h3>';
    } else {

    echo '
    <html>
    <body style="background-color:#D9D9D9">
    <form method="POST" action="">
    <input type="text" name="userName">
    <button type="submit">Click here</button>

    </body>
    </html>

    ';
    }

Page 1 of 2 12 LastLast

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •