[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

Get variable , ip address, computername in php ?

Posted on 2011-09-14
4
Medium Priority
?
390 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 59

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 59

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

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

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

For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
This article discusses how to create an extensible mechanism for linked drop downs.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to dynamically set the form action using jQuery.
Suggested Courses

656 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