Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding IP address to my form

I have this php code.

<?php
      if (isset($_POST['name'], $_POST['post'])) {
             $cast = $_POST['cast'];
             $name = $_POST['name'];
             $email = $_POST['email'];
             $post = nl2br ($_POST['post']);
             $ipaddress = $_POST['ipaddress'];

if (empty($name) or empty($post)) {
             $error = 'All Fields Are Required!';
}else{
$query = $pdo->prepare('INSERT INTO comments (cast, name, email, post, ipaddress) VALUES(?, ?, ?, ?, ?)');
     $query->bindValue(1, $cast);
     $query->bindValue(2, $name);
     $query->bindValue(3, $email);
     $query->bindValue(4, $post);
     $query->bindValue(5, $ipaddress);

     $query->execute();
?>

And this form.

<?php if (isset($error)) { ?>
     <small style="color:#aa0000;"><?php echo $error; ?></small><br /><br />
<?php } ?>

<form action="episode.php?id=<?php echo $data['cast_id']; ?>" method="post" autocomplete="off" enctype="multipart/form-data">
<input type="text" name="name" placeholder="Name" /> / <input type="text" name="email" placeholder="Email" /><small style="color:#aa0000;">*</small><br /><br />
<textarea rows="10" cols="50" name="post" placeholder="Comment"></textarea><br /><br />
<input type="submit" value="Add Comment" />
<br /><br />
<small style="color:#aa0000;">* <b>Email will not be displayed publicly</b></small><br />
</form>

As you can see I have set up an IP address to be saved with this form in my database.

How can I add the IP address in the form? But I do not want this to be displayed to my users.

Is this possible?

Thank you.

like image 790
kevstarlive Avatar asked Dec 04 '25 14:12

kevstarlive


1 Answers

perhaps you would replace:

$ipaddress = $_POST['ipaddress'];

by

$ipaddress = $_SERVER['REMOTE_ADDR'];

like image 190
Abu Romaïssae Avatar answered Dec 06 '25 05:12

Abu Romaïssae