?
Solved

Get variable , ip address, computername in php ?

Posted on 2011-09-14
4
Medium Priority
?
355 Views
Last Modified: 2012-05-12
Hi,

I plan to sell some streaming educational videos on internet. These videos will be embedded in an PHP file on my server. A specific variable is available called $userid from some other application. I want to put this $userid with ip address, computername and if possible user login name and put it in a table in database. This way I can record who is accessing this page.

Is this possible ? Can somebody give sample code ?
0
Comment
Question by:Vaibhav_joshi
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 58

Accepted Solution

by:
Julian Hansen earned 2000 total points
ID: 36535828
first create a database on a server (assuming MySQL)
Create a table called tbldownloads (or something)
// modify to your needs (not meant to be a script)
create tbldownloads (
id int PRIMARY_KEY AUTOINCREMENT NOT NULL,
userid char(20),
ipaddress char[20],
downloaded timestamp default CURRENT_TIMESTAMP,
mediaid int
);

// php
// change to match your server
define('gb_server', 'server');
define('gb_user','user');
define('gb_password',password');
define('gb_database','database_name');

$db = @mysql_connect(gb_server,gb_user,gb_password);
@mysql_select_db(gb_database) or die( "Unable to select database");

$ipaddress = isset($_SERVER['REMOTE_ADDR'])?$_SERVER['REMOTE_ADDR']:(isset($_SERVER['REMOTE_HOST'])?$_SERVER['REMOTE_HOST']:'undefined');

$userid = '' wherever you get this from " ;

$mediaid = "whatever the media id is for the download - get from URL or wherever";

$query = "INSERT INTO tbldownload(userid, ipaddress, mediaid) VALUES('$userid','$ip','$mediaid')";

$result = mysql_query($query);

// check result for errors and any other code you need
0
 
LVL 3

Expert Comment

by:garykristen
ID: 36535881


Have a look at the INET_NTOA and INET_ATON functions in mysql. They convert between dotted notation IP address to 32 bit integers. This allows you to store the IP in just 4 bytes rather than a 15 bytes. Make sure you use an unsigned int and not a signed int.
0
 
LVL 58

Expert Comment

by:Julian Hansen
ID: 36536001
yes and you can use ip2long as well but you can worry about that later - for now get your basic functionality up and running - saving 11 bytes of data you can implement when you start selling thousands of downloads
0
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 36536481
This article describes how to handle client authentication using a data base with user name and password.
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_2391-PHP-login-logout-and-easy-access-control.html

The IP address is available in $_SERVER["REMOTE_ADDR"].  You can find many other interesting things if you run the script in the code snippet.

The "computername" is not a term of art in PHP programming and is probably not available to your script.

This is a really good book that teaches many of the ways that PHP and MySQL interact.  Very readable with great examples.  Now in its fourth printing, it has been a permanent part of my professional library since Version One.
http://www.sitepoint.com/books/phpmysql4/

Best regards, ~Ray
<?php phpinfo(); // YEP, THAT IS ALL THERE IS TO THIS SCRIPT!

Open in new window

0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to count occurrences of each item in an array.
Suggested Courses

765 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question