Solved

Get variable , ip address, computername in php ?

Posted on 2011-09-14
4
334 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 57

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 57

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 110

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

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.
Learn about the eCommerce marketing trends for the year ahead.
The viewer will learn how to dynamically set the form action using jQuery.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

734 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