Solved

Get variable , ip address, computername in php ?

Posted on 2011-09-14
4
329 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
  • 2
4 Comments
 
LVL 52

Accepted Solution

by:
Julian Hansen earned 500 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 52

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 108

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Read about how to choose the best possible content marketing agency to suit your needs. Content marketing has become an integral part of running a successful tech business, so it is wise to be informed.
Learn about the eCommerce marketing trends for the year ahead.
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…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

914 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now