Solved

php display welcome message

Posted on 2014-01-21
8
241 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
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
These days socially coordinated efforts have turned into a critical requirement for enterprises.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to count occurrences of each item in an array.

744 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

13 Experts available now in Live!

Get 1:1 Help Now