Solved

php display welcome message

Posted on 2014-01-21
8
250 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
[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
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 38

Expert Comment

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

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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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
 
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 110

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying 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

Suggested Solutions

Title # Comments Views Activity
MySQL limit and not so limited 13 62
deprecated mysql extensions 1 55
[form-control] Retain values after a POST action 21 76
Considerations PHP using multiple locations 9 34
These days socially coordinated efforts have turned into a critical requirement for enterprises.
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
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…
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 …

710 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