Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

php display welcome message

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

Author Comment

by:XK8ER
ID: 39798762
I know I can do this with cookies but for this sample (no cookies)
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Question has a verified solution.

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

Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
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…
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 count occurrences of each item in an array.
Suggested Courses

916 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