/    Sign up×
Community /Pin to ProfileBookmark

Session with IP

If I have search form and track users with session, can be added session function and store this into database=

Is this correct way to track all searches from particular users (logged in or logged out).

[code=php]

session_start();
$_SESSION[‘id’] = $_SERVER[‘REMOTE_ADDR’];
[/code]

Names are now known sometimes and I pull those values for particular IP address…

to post a comment
PHP

12 Comments(s)

Copy linkTweet thisAlerts:
@gavshouseJan 26.2011 — Well since an IP address doesn't mean one person then no, for example if im surfing at my college all our computers use the same IP address therefore you dont really know.

Best way is to have a username and password system then you can authenticate the user on one machine only then you know.

Or maybe write a cookie to the users machine then for each page read it and check, say your tracking the web traffic on your site have something like this

[code=php]
<?php
$cookieData = //read cookie data
array_push($cookieData, $_SERVER['PHP_SELF']);
?>
[/code]


That way you keep adding the current page they are on to the array and maybe just write to the DB later.

This is just a simple example btw
Copy linkTweet thisAlerts:
@toplisekauthorJan 26.2011 — Is correct setting cookie?

[code=php]
session_start();
setcookie('search','cusearch',time()+60*60*24*7);
[/code]


How to add to database this user if he returns after 5 days?
Copy linkTweet thisAlerts:
@gavshouseJan 26.2011 — Is correct setting cookie?

[code=php]
session_start();
setcookie('search','cusearch',time()+60*60*24*7);
[/code]


How to add to database this user if he returns after 5 days?[/QUOTE]


You dont need session_start() for the cookie to work.

Okay just put this together quickly, this will keep writing the current date/time they visited and if 5 days or greater will write to the DB you can insert your DB stuff there.

[code=php]<?php
//okay so lets check if the cookie exists and has a numeric value
if(isset($_COOKIE['tracker'])) && (is_numeric($_COOKIE['tracker']))){
//lets now check if its older then five days
if($_COOKIE['tracker'] <= time()-(3600*24*5)){
//ok its older then 5 five days lets insert

//insert into DB here
}
}

//get the current unixstamp
$visitDate = time();
//set cookie for 31days
cookie('tracker', $visitDate, time()+3600*24*31, '/');

?>[/code]
Copy linkTweet thisAlerts:
@eval_BadCode_Jan 26.2011 — [code=php]session_start();
$_SESSION['id'] = $_SERVER['REMOTE_ADDR']; [/code]



fixed

[code=php]
session_start();
[/code]


It will not work if they refuse the cookie. IP address has nothing to do with it, it's about the session_ID they store in the cookie. I refuse most cookies, they're mostly for advertisers and google. So plan on having people refuse them.
Copy linkTweet thisAlerts:
@toplisekauthorJan 26.2011 — 
It will not work if they refuse the cookie. IP address has nothing to do with it, it's about the session_ID they store in the cookie. I refuse most cookies, they're mostly for advertisers and google. So plan on having people refuse them.[/QUOTE]


What code would be than suitable to track users that are not logged and they search?

I will be more specific. When search term is executed all search terms are stored into db. When user comes within 30 days it will check cookie.

  • 1. Issue is to follow users what they search but some come again within 30 days - [B]more visits from the same visitor[/B]...

  • 2. Issue is that some visitors make [B]more searches during visit[/B]. If I detect session value for this search I can track user for particular visit and with cookie searches within 30 days.


  • What is correct code to do this?

    I was not specific and I apologise. I did not mention users but visitors (not logged system)...
    Copy linkTweet thisAlerts:
    @Jarrod1937Jan 26.2011 — If you just want to track visitors for up to 30 days why not just use sessions and extend the session time up to 30 days? For this you can just extend the cookie lifetime and session lifetime values.
    Copy linkTweet thisAlerts:
    @toplisekauthorJan 27.2011 — Is this correct?

  • 1. How you set sessions and extend the session time up to 30 days?

    Is not session only one time for visit?

  • 2. How to store into db values for cookie as you posted value: $visitDate

    If you have more visitors it will be the same value for cookie?


  • [code=php]
    $visitDate = time();
    cookie('cutracker', $visitDate, time()+3600*24*30, '/'); //set cookie for 30days

    if(isset($_COOKIE['cutracker'])) && (is_numeric($_COOKIE['cutracker']))){
    //lets now check if its older then 30 days
    if($_COOKIE['cutracker'] <= time()-(3600*24*30)){
    //ok its older then 30 five days lets insert

    //insert into DB here
    }
    }
    [/code]
    Copy linkTweet thisAlerts:
    @gavshouseJan 27.2011 — Is this correct?

  • 1. How you set sessions and extend the session time up to 30 days?

    Is not session only one time for visit?

  • 2. How to store into db values for cookie as you posted value: $visitDate

    If you have more visitors it will be the same value for cookie?


  • [code=php]
    $visitDate = time();
    cookie('cutracker', $visitDate, time()+3600*24*30, '/'); //set cookie for 30days

    if(isset($_COOKIE['cutracker'])) && (is_numeric($_COOKIE['cutracker']))){
    //lets now check if its older then 30 days
    if($_COOKIE['cutracker'] <= time()-(3600*24*30)){
    //ok its older then 30 five days lets insert

    //insert into DB here
    }
    }
    [/code]
    [/QUOTE]


    Why have you switched around the code ? its now not going to work as you want, also this is cookies not sessions.

    I would look at this http://w3schools.com/php/php_mysql_insert.asp

    You want to check if the cookie exists and insert before you try to write a new cookie since it will be faster.
    Copy linkTweet thisAlerts:
    @toplisekauthorJan 27.2011 — So, correct code to track visitor and searches is the following:
    [code=php]
    if(isset($_COOKIE['cutracker']) && (is_numeric($_COOKIE['cutracker']))) //if the cookie exists and has a numeric value
    {

    if($_COOKIE['cutracker'] <= time()-(3600*24*30)) //check if its older then 30 days
    {
    $cur_cookie = $_COOKIE['cutracker'];
    $i=0; // start counter
    $keytofind = 1; // the position we want to find the key for $key = ''; // the key

    foreach($_GET as $k => $v){
    if($i == $keytofind){$key = $k;}
    $i++;
    }

    $ststring = $_GET[q]; // Store only search queries
    $sql = "INSERT INTO tbl_searchterms (id,cur_cookie,cur_timestamp,cur_datetime,key,ststring ,stip) ";
    $sql .= "VALUES ('','$cur_cookie','$cur_timestamp','$cur_datetime', '$key', '$ststring','$ip_address')";
    $rs = mysql_query($sql) or die(mysql_error());
    mysql_close();
    }
    }
    if (!isset($_COOKIE['cutracker']))
    {
    $visitDate = time(); //get the current unixstamp
    cookie('cutracker', $visitDate, time()+3600*24*30, '/');//set cookie for 30 days
    $i=0; // start counter
    $keytofind = 1; // the position we want to find the key for $key = ''; // the key

    foreach($_GET as $k => $v){
    if($i == $keytofind){$key = $k;}
    $i++;
    }

    $ststring = $_GET[q]; // Store only search queries
    $sql = "INSERT INTO tbl_searchterms (id,cur_cookie,cur_timestamp,cur_datetime,key,ststring ,stip) ";
    $sql .= "VALUES ('','','$cur_timestamp','$cur_datetime', '$key', '$ststring','$ip_address')";
    $rs = mysql_query($sql) or die(mysql_error());
    mysql_close();

    }
    ?>
    [/code]


    I'm not sure but your code shows blank page.

    If there is not cookie it will show just blank page.

    Is issue in inserted values into table as cookie has also other part (not defined cookie)...You see last IF has not stored $cur_cookie and the first one has $cur_cookie = $_COOKIE['cutracker'];

    I have tested and last IF has code like:

    // cookie('cutracker', $visitDate, time()+3600*24*30, '/');//set cookie for 30 days

    // $cur_cookie = $_COOKIE['cutracker'];

    when I remove this code it will work search engine.

    What can be wrong in code?
    Copy linkTweet thisAlerts:
    @Jarrod1937Jan 27.2011 — Sessions are where php sends the browser a cookie containing a session id and allows for a super global array ($_SESSION) as the interface for data storage. In reality, with the default session handler, the session information is saved as flat text files within the servers tmp directory. A garbage collection routine runs based on the session gc settings and deletes sessions that have expired past the max lifetime setting. However, you can extend the session lifetime via:

    http://php.net/manual/en/function.session-set-cookie-params.php

    Which will let you set the specific settings for the session cookie, including its expiration time. But with that you also need to extend the session lifetime itself via:

    http://www.php.net/manual/en/session.configuration.php#ini.session.gc-maxlifetime

    If you set both of these for up to 30 days you'll be sure to track each unique user (at least unique to their browser holding the session id) for up to 30 days.
    Copy linkTweet thisAlerts:
    @toplisekauthorJan 27.2011 — Can you change my code to this please? I'mcompletely new to this?
    Copy linkTweet thisAlerts:
    @Jarrod1937Jan 27.2011 — Sorry, but i won't make the code for you. If you're new, you'll only get better by programming it for yourself. Research those settings i just gave and you should be able to find plenty of examples.
    ×

    Success!

    Help @toplisek spread the word by sharing this article on Twitter...

    Tweet This
    Sign in
    Forgot password?
    Sign in with TwitchSign in with GithubCreate Account
    about: ({
    version: 0.1.9 BETA 5.20,
    whats_new: community page,
    up_next: more Davinci•003 tasks,
    coming_soon: events calendar,
    social: @webDeveloperHQ
    });

    legal: ({
    terms: of use,
    privacy: policy
    });
    changelog: (
    version: 0.1.9,
    notes: added community page

    version: 0.1.8,
    notes: added Davinci•003

    version: 0.1.7,
    notes: upvote answers to bounties

    version: 0.1.6,
    notes: article editor refresh
    )...
    recent_tips: (
    tipper: @AriseFacilitySolutions09,
    tipped: article
    amount: 1000 SATS,

    tipper: @Yussuf4331,
    tipped: article
    amount: 1000 SATS,

    tipper: @darkwebsites540,
    tipped: article
    amount: 10 SATS,
    )...