Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

php display welcome message

Posted on 2014-01-21
8
Medium Priority
?
252 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 1

Author Comment

by:XK8ER
ID: 39798762
I know I can do this with cookies but for this sample (no cookies)
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 58

Accepted Solution

by:
Gary earned 1000 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 1000 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 111

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: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

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

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
This article discusses how to create an extensible mechanism for linked drop downs.
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…

705 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