Solved

Get variable , ip address, computername in php ?

Posted on 2011-09-14
4
328 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 51

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 51

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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Every business owner understands the significance of online customer reviews and the impact it can have on sales and revenues. With technology advancing at such a rapid pace, getting online reviews has never been easier, especially when many regions…
This article discusses how to create an extensible mechanism for linked drop downs.
Viewers will get an overview of the benefits and risks of using Bitcoin to accept payments. What Bitcoin is: Legality: Risks: Benefits: Which businesses are best suited?: Other things you should know: How to get started:
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

706 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

11 Experts available now in Live!

Get 1:1 Help Now