how to save ip address of user in session

hi everyone !!

i am creating a polling website but i need some help, i want to run session for 3 hours in which whenever user click on vote button his ip address get saved and then he cannot vote for another 3 hours and then session destroys and again new session is created. i tried some codes such as
$_SESSION[‘ip’] = $_SERVER[‘REMOTE_ADDR’]; //to save ip address but i dont know how to run filter so that same user cannot vote for 3 hour. Need Help Plz Plz

You will have to log the IP addresses to a database along with a time stamp.

plz can you help me how to do that i am new to php. thanks for helping.

I suggest you start here: https://www.codecademy.com/tracks/php

And then here: https://www.codecademy.com/courses/learn-sql

Thanks i will try to learn from there…thanks for helping

there is nothing given about session or cookies or ip

i have saved ip into database but now how to stop old ip address from voting

<?php $conn = mysql_connect("server","username","password");//server, username and password are your server address and access details if(!$conn) die("cannot connect to mysql server" . mysql_error()); mysql_select_db("database Name", $conn); $sql = "INSERT INTO table_name (IP_Address) VALUES(" . $_SERVER['REMOTE_ADDR'] . ")"; if(!mysql_query($sql,$conn)) die("ERROR: " .mysql_error()); mysql_close($con); ?>

Anaypareek, don’t save the ip in the session. No need to do that. Just grab the timestamp. Many ways
to do that. If you need to display the timestamp, I use it this way:
$timestamp = date( “Y-m-d H:i:s”);
Date and time formatted to be displayed. Save that in a “datetime” field in your database record for the
user’s IP. So, save timestamp, ip in a table. Then, when the user presses the vote button, before you
record the vote, query their IP address and grab the timestamp. (“last_time_voted” or something similar.)
Then do a compare of the timestamp to the current one using PHP’s date functions to see if it is too soon.
If too soon, tell the user so…

You can compare dates with days or hours differences using code something like this:
if ($last_vote_date"<=date(“Y-m-d H:i:s”, strtotime("-3 hours"))) { Display can’t vote yet notice… }
( Not tested, just an example… )

Some programmers might store the next available voting time instead of the time of voting. If you do that,
a simple compare to see if the current time is later than the stored one. This makes it easier. Now, you
could display a timer on the user’s vote page to show how long it is before they can vote again…

Hope that helps some…

plz tell me will this work or not

<?php $servername = "localhost"; $username = "username"; $password = "password"; $dbname = "myDB"; // Create connection $conn = new mysqli($localhost,$root,$session); // Check connection if ($conn->connect_error) { die("Connection failed: " . $conn->connect_error); } $ip=$_SERVER['REMOTE_ADDR']; $sql = SELECT ipaddress FROM iptable; $time = SELECT time FROM iptable; $d=mktime(12, 00, 00, 01, 01, 2014); if($ip==$sql) { if($time>2014-01-01 14:00:00pm) { echo "voted successfully"; } else { echo "please vote after two hours"; } } else { INSERT INTO iptable(ipaddress,time) VALUES("$ip","echo date("Y-m-d h:i:sa", $d);" ); }

Here is an often overlooked idea. Download a few voting scripts that list the features you want and see how other coders did it. When I first started coding that is all I did. Never read the manual, didn’t do tutorials, just tore other peoples code apart to see what made it work. I did the same thing with a baseball once and sliced my hand open when the scissors slipped.

haha yeah i will try to do that too but if u can tell me is this code correct or not ??

Well, yes and no… Yes on Kevin’s comments, no on your code…

First, you are using a UNIX time command. Is your server a Unix system? Doubtful it is. Don’t use
“mktime()”. It can be done, but, it is not the way it is done these days. Next, how is your time field in your
database set up? Is it a real “datetime” field as it should be or is it a string of characters? You can not do
an IF clause as you posted it without quotes around the data. If $time>a bunch of chars… Does not
work… Also, you said you wanted to compare a saved time to not allow voting within 3 hours. But, your
sample code appears to just be a hardcoded compare to a set number. The code I posted does what you
want if you study it and see how it is done.

I will take a guess that you might be a student wanting a solution without wanting to learn anything… If
so, study my code and look up the functions on PHP.net or W3Schools.com and learn how they are used.

Sponsor our Newsletter | Privacy Policy | Terms of Service