1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

How to use session?

Discussion in 'PHP' started by vianca12, Jun 12, 2012.

  1. #1
    I would like to use session for my website so i can track the user.But I got confuse on how to use it.
    Can someone please explain it in details for me?:confused:
     
    vianca12, Jun 12, 2012 IP
  2. dujmovicv

    dujmovicv Member

    Messages:
    62
    Likes Received:
    2
    Best Answers:
    4
    Trophy Points:
    48
    #2
    dujmovicv, Jun 12, 2012 IP
  3. goldensea80

    goldensea80 Well-Known Member

    Messages:
    422
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    128
    #3
    Basically, at the beginning of every php script you should start the session by
    session_start();
    PHP:
    A session variable will persist even you click from pages to pages or refresh pages. A session variable can be define as
    $_SESSSION['myvar']=$_SESSSION['myvar']+1;
    echo $_SESSSION['myvar'];
    PHP:
    For example, the above code will show increasing number each time you refresh your page.
    But of course, you should google for more practical examples.
     
    goldensea80, Jun 13, 2012 IP
  4. vianca12

    vianca12 Peon

    Messages:
    12
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    So, i need to put this code for every page of my website?
    
    session_start()
    
    Code (markup):
     
    vianca12, Jun 13, 2012 IP
  5. ssmm987

    ssmm987 Member

    Messages:
    180
    Likes Received:
    4
    Best Answers:
    3
    Trophy Points:
    43
    #5
    Yes, indeed
     
    ssmm987, Jun 13, 2012 IP
  6. eritrea1

    eritrea1 Active Member

    Messages:
    182
    Likes Received:
    9
    Best Answers:
    2
    Trophy Points:
    70
    #6
    You need to put that code just to start a session. But you can not store anything with it, without the complete set as goldensea80 has suggested.


    
    <?php
    
    session_start();
    if(isset($_SESSION['view'])) 
    
       $_SESSION['view'] = $_SESSION['view']+1;
    else
       $_SESSION['view'] = 1;
    
    
    Code (markup):
    *Forexample if you put that code in your say aboutsession.php file, and then


    <?php echo $_SESSION['view']; ?>

    will give you the number of refresh a user is making in your site.
    it willl give each person who is viewing aboutsession.php
    everytime a person refreshed the browser, the value will incremebt.
    It will also help you, when your visitors in your site have to post something to a file/sql in your site it will help you to control the number of posts instead of having people post the same thing over and over, everytime they refresh the page (that happens in php)

    So, use that script to give each of ur users a uniq id


    good luck
     
    eritrea1, Jun 13, 2012 IP
  7. bragg

    bragg Peon

    Messages:
    7
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    session starts with browser opening and closes with browser closing
     
    bragg, Jun 14, 2012 IP
  8. Mispero

    Mispero Greenhorn

    Messages:
    43
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    16
    #8
    session end
    30 minutes after last user interaction with page.
     
    Mispero, Jun 14, 2012 IP
  9. vianca12

    vianca12 Peon

    Messages:
    12
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #9
    Thank you everybody for the help!
    
    <?php 
    ini_set('display_errors', 1); 
    error_reporting(E_ALL);
    session_start(); 
    include('dbconn.php');
    if(isset($_SESSION['A_id']))
    {
    ?>
    
    Code (markup):
    -this is the code that i put at the top of every page in my website. It is correct?
    Should I change ($_SESSION['id'] to [$_SESSION['username']?
    or it doesn't matter which attribute that i want to use?
     
    vianca12, Jun 14, 2012 IP
  10. Rukbat

    Rukbat Well-Known Member

    Messages:
    2,908
    Likes Received:
    37
    Best Answers:
    51
    Trophy Points:
    125
    #10
    Many people make this mistake. The website is never told that the browser is being closed, or is going to another site. Either you force the information (using onUnLoad() or one of a few other Javascript event handlers), you set a timeout for the session or you just let the session time out after the default 30 minutes.

    The web runs disconnected - the site doesn't know what the browser does, other than for the browser telling the site that it wants something. (And "I'm leaving now" isn't something the browser tells the site, since there's no reason for it.)
     
    Rukbat, Jun 15, 2012 IP