Results 1 to 14 of 14

Thread: Recording user details and writing them to a text file

  1. #1
    Hexus.Lurker
    Join Date
    Jan 2010
    Posts
    357
    Thanks
    17
    Thanked
    13 times in 13 posts
    • Fortune117's system
      • Motherboard:
      • Gigabyte Z77-D3H
      • CPU:
      • Intel Core i5 3570k 3.4Ghz
      • Memory:
      • 8GB Corsair DDR3
      • Storage:
      • Kingston Hyper X 120GB SSD
      • Graphics card(s):
      • XFX Radeon 7870 DD Edition
      • PSU:
      • OCZ ZS Series 650 Watt
      • Case:
      • Coolermaster CM690
      • Operating System:
      • Windows 8 64-Bit
      • Monitor(s):
      • [Eyefinity] 3 LG 23EA63V
      • Internet:
      • Virgin Media 50MB

    Recording user details and writing them to a text file

    Hi guys,

    Currently doing a little project for college where I need to get various pieces of user information (such as Screen resolution, browser type and IP address) and then write this information to a text file. So far I have something that looks like this that has been pieced together from various bits of work

    Code:
    <?php
    	function get_user_browser() {
    
    		$u_agent = $_SERVER['HTTP_USER_AGENT'];
    		$ub="";
    
    	        if(preg_match('/MSIE/i',$u_agent)) {
    			$ub="ie";
    			}
    		elseif(preg_match('/firefox/i', $u_agent)) {
    			$ub="firefox";
    			}
    		elseif(preg_match('/safari/i', $u_agent)) {
    			$ub="safari";
    			}
    		elseif(preg_match('/opera/i', $u_agent)) {
    			$ub="opera";
    			}
    		elseif(preg_match('/chrome/i', $u_agent)) {
    			$ub="chrome";
    			}
    			
    		return $ub;
    		}
    
    		$myFile="analyse.txt";
    		$fileHandle= fopen ($myFile, 'a');
                    $info="$ub, $_COOKIE['height'], $_COOKIE['width']\n";
                    fwrite($fileHandle, $info);
    				fclose($fileHandle);
    ?>
    Could somebody tell me where im going wrong and possibly point be in the right direction here?

    Many thanks,

    Fortune

  2. #2
    Seething Cauldron of Hatred TheAnimus's Avatar
    Join Date
    Aug 2005
    Posts
    17,168
    Thanks
    803
    Thanked
    2,152 times in 1,408 posts

    Re: Recording user details and writing them to a text file

    Could you not use google analytics or similar?

    What is currently going wrong? Are you getting the file with any output?
    throw new ArgumentException (String, String, Exception)

  3. #3
    HEXUS.social member Agent's Avatar
    Join Date
    Jul 2003
    Location
    Internet
    Posts
    19,185
    Thanks
    739
    Thanked
    1,614 times in 1,050 posts

    Re: Recording user details and writing them to a text file

    Your problem is:
    PHP Code:
    $info="$ub$_COOKIE['height'], $_COOKIE['width']\n"
    You have a variable called $info and then you're trying to give it 3 things at once.

    I'm assuming you want the string appended, given that fwrite only takes a string as the second parameter. If so, you want it in this format:

    PHP Code:
    $var1 'Hex';
    $var2 'us';

    $var3 $var1 $var2;

    echo(
    $var3);

    //would print "Hexus" 
    So for yours:

    PHP Code:
    $info="$ub$_COOKIE['height'] . $_COOKIE['width'] . '\n'
    You could now use $info for fwrite.

    Keep in mind that Cookie data is not safe. Sanitise it and make sure it's what your expecting, certainly if you ever want to write this to a database. Something like is_numeric() will work nicely for most things here, but also have a look at ctypes and special characters.
    Last edited by Agent; 30-05-2012 at 10:46 PM.

  4. Received thanks from:

    Fortune117 (30-05-2012)

  5. #4
    Seething Cauldron of Hatred TheAnimus's Avatar
    Join Date
    Aug 2005
    Posts
    17,168
    Thanks
    803
    Thanked
    2,152 times in 1,408 posts

    Re: Recording user details and writing them to a text file

    but if he has magic quotes on that would work fine....
    throw new ArgumentException (String, String, Exception)

  6. Received thanks from:

    Fortune117 (30-05-2012)

  7. #5
    HEXUS.social member Agent's Avatar
    Join Date
    Jul 2003
    Location
    Internet
    Posts
    19,185
    Thanks
    739
    Thanked
    1,614 times in 1,050 posts

    Re: Recording user details and writing them to a text file

    No PHP thread is complete until TheAnimus has posted

  8. #6
    Hexus.Lurker
    Join Date
    Jan 2010
    Posts
    357
    Thanks
    17
    Thanked
    13 times in 13 posts
    • Fortune117's system
      • Motherboard:
      • Gigabyte Z77-D3H
      • CPU:
      • Intel Core i5 3570k 3.4Ghz
      • Memory:
      • 8GB Corsair DDR3
      • Storage:
      • Kingston Hyper X 120GB SSD
      • Graphics card(s):
      • XFX Radeon 7870 DD Edition
      • PSU:
      • OCZ ZS Series 650 Watt
      • Case:
      • Coolermaster CM690
      • Operating System:
      • Windows 8 64-Bit
      • Monitor(s):
      • [Eyefinity] 3 LG 23EA63V
      • Internet:
      • Virgin Media 50MB

    Re: Recording user details and writing them to a text file

    Thanks for the replies guys

    Quote Originally Posted by TheAnimus View Post
    Could you not use google analytics or similar?

    What is currently going wrong? Are you getting the file with any output?
    The question from the assignment states:

    "Using PHP, create a web application to generate website statistics"

    So I dont think Google Analytics is accepted so I thought that I could develop something using PHP that will output some basic user details to a text file such as IP address, Screen resolution, Browser etc etc.

    Again, thanks a lot for the replies guys

  9. #7
    Hexus.Lurker
    Join Date
    Jan 2010
    Posts
    357
    Thanks
    17
    Thanked
    13 times in 13 posts
    • Fortune117's system
      • Motherboard:
      • Gigabyte Z77-D3H
      • CPU:
      • Intel Core i5 3570k 3.4Ghz
      • Memory:
      • 8GB Corsair DDR3
      • Storage:
      • Kingston Hyper X 120GB SSD
      • Graphics card(s):
      • XFX Radeon 7870 DD Edition
      • PSU:
      • OCZ ZS Series 650 Watt
      • Case:
      • Coolermaster CM690
      • Operating System:
      • Windows 8 64-Bit
      • Monitor(s):
      • [Eyefinity] 3 LG 23EA63V
      • Internet:
      • Virgin Media 50MB

    Re: Recording user details and writing them to a text file

    Oh, and its throwing up this error:

    Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in /homepages/4/d399133263/htdocs/live/ip.php on line 28

    Ln 28 = $info="$ub, $_COOKIE['height'], $_COOKIE['width']\n";

  10. #8
    HEXUS.social member Agent's Avatar
    Join Date
    Jul 2003
    Location
    Internet
    Posts
    19,185
    Thanks
    739
    Thanked
    1,614 times in 1,050 posts

    Re: Recording user details and writing them to a text file

    Well of course it is if you've not changed your code. Read what I wrote above - it works fine

  11. #9
    Hexus.Lurker
    Join Date
    Jan 2010
    Posts
    357
    Thanks
    17
    Thanked
    13 times in 13 posts
    • Fortune117's system
      • Motherboard:
      • Gigabyte Z77-D3H
      • CPU:
      • Intel Core i5 3570k 3.4Ghz
      • Memory:
      • 8GB Corsair DDR3
      • Storage:
      • Kingston Hyper X 120GB SSD
      • Graphics card(s):
      • XFX Radeon 7870 DD Edition
      • PSU:
      • OCZ ZS Series 650 Watt
      • Case:
      • Coolermaster CM690
      • Operating System:
      • Windows 8 64-Bit
      • Monitor(s):
      • [Eyefinity] 3 LG 23EA63V
      • Internet:
      • Virgin Media 50MB

    Re: Recording user details and writing them to a text file

    Quote Originally Posted by Agent View Post
    Well of course it is if you've not changed your code. Read what I wrote above - it works fine
    Forgot to save! haha Anyway, it seems that this is just writing /n to the output file. hmm..

  12. #10
    Registered User
    Join Date
    Mar 2010
    Posts
    5
    Thanks
    0
    Thanked
    3 times in 2 posts

    Re: Recording user details and writing them to a text file

    Call the function rather than trying to use $ub (only exists within the function) and it should work.

    PHP Code:
    $info sprintf("%s, %d, %d\n"get_user_browser(), $_COOKIE['height'], $_COOKIE['width']); 


    Quote Originally Posted by Agent View Post
    If so, you want it in this format:

    PHP Code:
    $var1 'Hex';
    $var2 'us';

    $var3 $var1 $var3;

    echo(
    $var3);

    //would print "Hexus" 
    Notice: Undefined variable: var3 in ... on line 4
    Hex
    Last edited by Digby; 30-05-2012 at 07:50 PM.

  13. Received thanks from:

    Agent (30-05-2012)

  14. #11
    Hexus.Lurker
    Join Date
    Jan 2010
    Posts
    357
    Thanks
    17
    Thanked
    13 times in 13 posts
    • Fortune117's system
      • Motherboard:
      • Gigabyte Z77-D3H
      • CPU:
      • Intel Core i5 3570k 3.4Ghz
      • Memory:
      • 8GB Corsair DDR3
      • Storage:
      • Kingston Hyper X 120GB SSD
      • Graphics card(s):
      • XFX Radeon 7870 DD Edition
      • PSU:
      • OCZ ZS Series 650 Watt
      • Case:
      • Coolermaster CM690
      • Operating System:
      • Windows 8 64-Bit
      • Monitor(s):
      • [Eyefinity] 3 LG 23EA63V
      • Internet:
      • Virgin Media 50MB

    Re: Recording user details and writing them to a text file

    Just having a ponder at the assignment question and then came across this description on wikipeida..

    Web analytics provides information about the number of visitors to a website and the number of page views. It helps gauge traffic and popularity trends which is useful for market research.

    So wouldnt I just need to include the amount of visitors on a certain day and an amount of visitors for a month?

    -Fortune

  15. #12
    HEXUS.social member Agent's Avatar
    Join Date
    Jul 2003
    Location
    Internet
    Posts
    19,185
    Thanks
    739
    Thanked
    1,614 times in 1,050 posts

    Re: Recording user details and writing them to a text file

    Quote Originally Posted by Digby View Post
    Notice: Undefined variable: var3 in ... on line 4
    Hex
    A rouge 3 appeared!
    The wonders of trying to write code on a mobile phone

  16. #13
    Hexus.Lurker
    Join Date
    Jan 2010
    Posts
    357
    Thanks
    17
    Thanked
    13 times in 13 posts
    • Fortune117's system
      • Motherboard:
      • Gigabyte Z77-D3H
      • CPU:
      • Intel Core i5 3570k 3.4Ghz
      • Memory:
      • 8GB Corsair DDR3
      • Storage:
      • Kingston Hyper X 120GB SSD
      • Graphics card(s):
      • XFX Radeon 7870 DD Edition
      • PSU:
      • OCZ ZS Series 650 Watt
      • Case:
      • Coolermaster CM690
      • Operating System:
      • Windows 8 64-Bit
      • Monitor(s):
      • [Eyefinity] 3 LG 23EA63V
      • Internet:
      • Virgin Media 50MB

    Re: Recording user details and writing them to a text file

    Okay, going on this new idea that ive had ive come up with this

    www.aviodesign.co.uk/live/index.php

    My question is how would I get monthly and weekly views?

  17. #14
    Hexus.Lurker
    Join Date
    Jan 2010
    Posts
    357
    Thanks
    17
    Thanked
    13 times in 13 posts
    • Fortune117's system
      • Motherboard:
      • Gigabyte Z77-D3H
      • CPU:
      • Intel Core i5 3570k 3.4Ghz
      • Memory:
      • 8GB Corsair DDR3
      • Storage:
      • Kingston Hyper X 120GB SSD
      • Graphics card(s):
      • XFX Radeon 7870 DD Edition
      • PSU:
      • OCZ ZS Series 650 Watt
      • Case:
      • Coolermaster CM690
      • Operating System:
      • Windows 8 64-Bit
      • Monitor(s):
      • [Eyefinity] 3 LG 23EA63V
      • Internet:
      • Virgin Media 50MB

    Re: Recording user details and writing them to a text file

    For those interested the finished product is Here. A video that shows how I developed the Hit counter.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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