Solved

php display welcome message

Posted on 2014-01-21
8
243 Views
Last Modified: 2014-01-21
Hello,
I would like to display a welcome message and store the IP address of the client in a text file each (line by line) so that if the user browses through the website he will only get one message.


$ipAddress = file_get_contents('ip_address.txt', true);
$pieces = explode("\n", $ipAddress);
$iFound = false;

foreach ($pieces as $value) {
    if ($value == $raddr) {
    	echo "ip dup\n";
    	$iFound = true;
    	exit;    	
    }
}

if ($iFound == false) {
	echo "ip new\n";	
	file_put_contents("ip_address.txt",$raddr."\n",FILE_APPEND);
}

Open in new window


now my issue is that I can reset the whole file every 24hrs but if a user is on the website he would get the welcome message again.. what can I do so that it deletes older than 24hrs? maybe by adding timestamp|ip_address?
0
Comment
Question by:XK8ER
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 38

Expert Comment

by:Tom Beck
ID: 39798756
You mean instead of using cookies?
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 39798757
0
 
LVL 1

Author Comment

by:XK8ER
ID: 39798762
I know I can do this with cookies but for this sample (no cookies)
0
 
LVL 58

Accepted Solution

by:
Gary earned 250 total points
ID: 39798776
This is where you would need to be using a database, text files would become very messy and hard to administer. Whereas with a db you could do a quick check against the timestamp of the IP and decide whether to show the message.
0
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.

 
LVL 38

Assisted Solution

by:Tom Beck
Tom Beck earned 250 total points
ID: 39798798
Database would be better, but if it must be a text file, consider XML. Use the DOMDocument class to manipulate it.

http://www.php.net/manual/en/class.domdocument.php

That way, you can save the IP address and a timestamp with an expiration date. Then, for every page request, cull out the old and insert the new rather than reset the whole file every 24 hours.

Never actually did this myself however.
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 39798803
You need to use the HTTP cookies, full stop.  Many clients use services (example: AOL) that do not provide static IP addresses.  You can try to do this without cookies and you'll be successful for some of your clients.  And you'll fail with others.  The professional solution uses cookies.  The PHP session uses cookies.  It's easier to ride the horse in the direction he is already going.

Best of luck with your project, ~Ray
0
 
LVL 38

Expert Comment

by:Tom Beck
ID: 39798813
Sounds like a sample project to me, not a real world circumstance.
0
 
LVL 58

Expert Comment

by:Gary
ID: 39798818
In Europe you have to request permission to use cookies, and every website has to explicitly state they are using cookies and give you the option of not being tracked.  There is an exception to the rule where the use of cookies is required to use the site and you can use implied consent if you continue to use the site - though the law is still a bit murky around this.
Of course this would be a lot easier if all ISP's adopted IPv6 and just assigned a static IP to everyone (v6 has billions of available addresses) but then again it might make it easier to track people (search MS and NSA).
I don't know the situation in the US but in Europe if you have broadband then your IP is unlikely to change from year to year.
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

Suggested Solutions

Both Easy and Powerful How easy is PHP? http://lmgtfy.com?q=how+easy+is+php (http://lmgtfy.com?q=how+easy+is+php)  Very easy.  It has been described as "a programming language even my grandmother can use." How powerful is PHP?  http://en.wikiped…
Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

912 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

23 Experts available now in Live!

Get 1:1 Help Now