I need a code that displays the IP of the person/guest that commented on my website.
Here's the index page;
<?php
include "core/init.php";
?>
<!DOCTYPE html>
<link rel="stylesheet" type="text/css" href="http://hjalplista.comxa.com/daniel_emelie/style.Css">
<meta http-equiv="Content-Type" content="text/html;charset=utf-8">
<title>Välkommen till hjälplistan!</title>
<body>
<div id="header">
<div align="right"><img src="http://i.imgur.com/y0aiNIt.jpg"></div>
</div>
<div id="inside">
<center>
<div id="location"><a href='index.php'>Hem</a> - <a href='administrative/adminonlyaccess.php'><i>Admin</i></a><br><br>
</div>
<hr/>
<br>
<div id="text">
<br>
<table border="1" cellpadding="1" cellspacing="1" style="width: 100%;">
<tbody>
<tr>
<td><span style="color:slategray;"><strong><center>Nr</center></strong></span></td>
<td><span style="color:slategray;"><strong><s><center>Fått hjälp</center></s></strong></span></td>
<td><span style="color:slategray;"><strong><center>Namn</center></strong></span></td>
<td><span style="color:slategray;"><strong><center>Uppgift</center></strong></span></td>
<td><span style="color:slategray;"><strong><center>IP-Adress</center></strong></span></td>
</tr>
<?php
include "core/inc/conn.php";
mysql_select_db("comments");
$getquery = mysql_query("SELECT * FROM comments ORDER BY id DESC");
while($rows = mysql_fetch_assoc($getquery))
{
$id = $rows['id'];
$comment_name = $rows['name'];
$comment = $rows['comment'];
echo"<tr><th><font color='lightgray'>$id</font></th>";
echo"<th><font color='red'>Fungerar ej!</font></th>";
echo "<th><u><font color='black'>$comment_name</font></u></th>";
echo"<th><font face='black'>$comment</font></th>";
echo"<th></th></tr>";
}
if(isset($_GET['error']))
{
echo "<p>15 Bokstäver max!";
}
?>
</table>
<br><br>
</body>
<head>
<center>
</div>
<br><br>
<form action="post_comment.php" method="post">
<label> Namn: <input type="text" name="name"></label><br><br>
<label> Uppgift: <input type="text" name="comment"></label><br><br>
<input type="submit" name="post" value="Skicka">
</form>
<br><br>
<br><br>
<?php
echo "<br><br><br>";
echo "<hr/>";
echo "<p> © Wieselgrenskolan. All rights reserved. </p>";
?>
</head>
</html>
<html>
<div align="right">
<!-- START OF HIT COUNTER CODE -->
<br><script language="JavaScript" src="http://www.counter160.com/js.js?img=11"></script><br><img src="http://www.counter160.com/images/11/left.png" alt="Free web hosting" border="0" align="textright"><img alt="Web hosting" src="http://www.counter160.com/images/11/right.png" border="0" align="texttop">
<!-- END OF HIT COUNTER CODE -->
</div>
</html>
And it's supposed to be in the echo "<th></th></tr>";. Do I need a MySQL table for that?
I've created a index in mysql called ''commentip'' but I don't know how to fix the missing parts.
You can get the IP in php in most cases using $_SERVER['REMOTE_ADDR'] However if they are using a proxy or you are using a reverse proxy like cloudflare or incapsula then you can use $_SERVER['HTTP_X_FORWARDED_FOR'].
Related
I use the following:
<a href="showlog.php?id=<?php echo $car['car_id']; ?>">
and I get a url that looks like this:
https://example.com/showlog.php?id=AAA1111
So now in the showlog.php file I want select from a table all the lines that have this id value saved as car_id and echo the id (primary key which is different than the car_id), the trn_date, and the kilometers, so I use the following code:
<?php
include 'main.php';
checkLoggedIn($pdo);
$id=$_GET['id'];
$stmt = $pdo->prepare('SELECT id, trn_date, kilometers FROM serv WHERE car_id = '.$id.' ');
$stmt->execute([$_SESSION['id']]);
$cars = $stmt->fetchAll(PDO::FETCH_ASSOC);
?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body class="loggedin">
<div class="content">
<h2>Ser</h2>
<div>
<table>
<?php foreach($cars as $car): ?>
<tr>
<td>
<a href="showser.php?id=<?php echo "". $car["id"]. ""; ?>" style="color:DarkCyan; text-decoration: none">
<img src="images/car_service.png" width="40" height="40"> Date: <?="". $car["trn_date"]. ""?>
</a>
</td>
<td>kilometers: <?="". $car["kilometers"]. ""?></td>
</tr>
<tr><th colspan="8"><br/><hr width="100%"></th><tr>
<?php endforeach; ?>
</table>
</div>
</div>
</body>
</html>
The page loads but I get nothing as result. What am I doing wrong? :/
I am working on a website where I am getting input of semester number from the user and I want to display the subjects in the semester they entered accordingly.
For this, I planned to create tables for each semester which would have the subjects in it.
For now, I am successful to retrieve data from one table. But I am required to get subjects as per the entered semester by the student.
Following is the code,which works perfectly for semester 5. Please suggest alterations as per the requirements asked above.
<!DOCTYPE html>
<html>
<head>
<title></title>
<!-- very important -->
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="subject.css">
</head>
<body>
<h1 class="text-center text-capitalize font-weight-bold">Subject Information</h1>
<div class="container">
<div>
<br>
<div>
<form class="form-inline">
<!-- <div class="mx-auto" style="width: 200px;"> -->
<div class="form-group">
<label for="Sem"> Semester: </label>
<div>
<input type="text" name="sem_no" placeholder="Enter current semester" id=sem class="form-control">
</div>
<br>
</div>
</form>
</div>
</div>
</div>
<div>
<!-- TABLE DISPLAYED WITH DATA FROM RESPECTIVE DATABASE -->
<div class= "container">
<div class="col-lg-12 ">
<br><br>
<h2 class="text-center">Subjects</h2>
<table class="table table-striped table-hover table-bordered">
<tr>
<th>Subject code</th>
<th>Subject Name</th>
<th>Faculty name</th>
</tr>
<?php
$con = mysqli_connect('localhost','root');
mysqli_select_db($con,'subjects');
$q= "select * from sem5";
$query = mysqli_query($con,$q);
while($res = mysqli_fetch_array($query))
{
?>
<tr>
<td><?php echo $res['subject_no']; ?></td>
<td><?php echo $res['subject_name']; ?></td>
<td><?php echo $res['faculty_name']; ?></td>
</tr>
<?php
}
?>
</table>
</div>
</div>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.0/umd/popper.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js"></script>
</body>
</html>
Please suggest alterations. Will be highly obliged.
You need to do a HTTP GET request and use PHP to get the value. Then use PHP to make the SQL query. Here it is in its simplest form.
I've added a submit button which submits the form and executes the HTTP GET with the parameters attached to the URL. You will see ?sem_no=x after it is submitted. That's the Query String.
...
...
<div class="form-group">
<label for="Sem"> Semester: </label>
<div>
<input type="text" name="sem_no" placeholder="Enter current semester" id=sem class="form-control">
</div>
<div>
<input type="submit"/>
</div>
<br>
...
...
...
...
//check the value was submitted and it is not blank
<?php if(isset($_GET['sem_no']) && !empty($_GET['sem_no'])) { ?>
<!-- TABLE DISPLAYED WITH DATA FROM RESPECTIVE DATABASE -->
<div class= "container">
<div class="col-lg-12 ">
<br><br>
<h2 class="text-center">Subjects</h2>
<table class="table table-striped table-hover table-bordered">
<tr>
<th>Subject code</th>
<th>Subject Name</th>
<th>Faculty name</th>
</tr>
<?php
$con = mysqli_connect('localhost','root');
mysqli_select_db($con,'subjects');
$q= 'select * from sem'.$_GET['sem_no'];
$query = mysqli_query($con,$q);
while($res = mysqli_fetch_array($query))
{
?>
<tr>
<td><?php echo $res['subject_no']; ?></td>
<td><?php echo $res['subject_name']; ?></td>
<td><?php echo $res['faculty_name']; ?></td>
</tr>
<?php
}
?>
</table>
</div>
</div>
<?php
//closing if
} ?>
...
...
As mentioned in the comments earlier, this database design is quite poor. I know you are learning though. Have you considered having a subjects table that has a Semester column? Then you could do a query like:
SELECT * FROM subjects where semester_number = x
There are even better ways to do it beyond that but this would be a good starting point for you.
I did complete a website for online Entrance form for students as my demo project to learn. When I run from my local server it works fine but when I uploaded on webserver and tasted index.php and other files run fine except when user enter his/her symbol no to check if he/she already been registered or not..I have coded a logic
if (exists)>Show admit card
if(don't exist)>show alert box
It works fine in local server but in webserver when I enter value in search box and enter then it shows empty page with no any error.
I have one row in my database. So in case you wanna check here is the symbol no in column =15369-2017-02 . On Entering submit it should show admit card and you can enter any random value other then this .which should show alert box.
Here is my website
https://cmprc.edu.np/condensed/entrance_form_demo/studntreport/main/
This is the code of file which is not responding and showing blank
<html>
<head>
<title>
CDP || Admission Form
</title>
<link href="css/bootstrap.css" rel="stylesheet">
<link href="../style.css" media="screen" rel="stylesheet" type="text/css" />
<body style="background-color: white;">
<?php
include('../connect.php');
$var_value = $_POST['search_value'];
echo $var_value;
$sql="SELECT * FROM entrance WHERE re_value='$var_value'";
$STH = $db->prepare($sql);
$STH->execute(array($var_value));
$User = $STH->fetch();
if (empty($User))
echo "<script>alert('Sorry, you Have not Registered yet');
window.location = 'index.php';</script>";
else
$result = $db->prepare("SELECT * FROM entrance
WHERE re_value = '$var_value' ORDER BY id ASC;");
$result->execute();
for($i=0; $row = $result->fetch(); ){
?>
<link href="../style.css" media="screen" rel="stylesheet" type="text/css" />
<center><h4><i class="icon-edit icon-large"></i> You've Already Registered</h4></center>
<hr>
<center>
<div class="panel panel-default">
<div class="container">
<div class="row">
<div class="col-md-4 seventy">
<img src="img/admit.png"/ >
</div>
<div class="col-md-8 thirty">
<img src="../image/profile/<?php echo $row['pic_value'];?>" class="roundimage2" alt=""/>
</div>
</div>
</div>
<hr>
<table style=" width: 500px;">
<tr>
<td >Roll no: </td>
<td> <?php echo $row['id']; ?></td>
</tr>
<tr>
<td >Name : </td>
<td > <?php echo $row['name_en_value']; ?></td>
<td > Subject: </td>
<td > <?php echo $row['ge_value']; ?> </td>
</tr>
</table>
<br><br>
<h5><i><strong>STUDENTS MUST BRING THIS ADMIT CARD ON THE DAY OF EXAMINATION</strong></i></h5>
<br>
</div>
</center>
<?php
}
?>
</body>
<?php include('footer.php');?>
</html>
Any help? I did almost finished it which I was working continously for 4-5 Days.
It seems that your php is not executed correctly. Maybe try to enable error reporting to get a hint of what's wrong.
Include the following at the beginning of your destination file (my.php)
<?php
error_reporting(E_ALL);
ini_set("display_errors", 1);
i'm working on a form in php mysql. so in my page there is a form and below that i shown the data of that form in table format.now i want to print only the table structure thats why i just made a PRINT button as:
<span style="float:right;">PRINT</span>
but it will print the whole page with form field and table and i just want the tableto print.
Here is the design of my whole page with form and table:
<html>
<head></head>
<body>
<div class="container">
<form class="form-horizontal" action="" method="post" name="userform1" id="company-form" enctype="multipart/form-data">
<?php if($_GET[id]){?>
<fieldset>
<legend>Add Company</legend>
<div class="control-group">
<label class="control-label">Company Name</label>
<div class="controls">
<input type="text" name="company" id="company" value="<?php echo $selup['company']?>">
</div>
</div>
<div class="control-group">another field</div>
<div class="control-group">another field</div>
<div class="control-group">
<div class="controls">
<button type="submit" class="btn" name="submit" id="submit" value="Submit">Submit</button>
</div>
</div>
<table class="table table-bordered">
<tbody>
<tr>
<td>S.No.</td>
<td>Company Name</td>
<td>Type</td>
<td>Action</td>
</tr>
<?php
// to print the records
$select = "select * from company where type='Miscellaneous'";
$query1 = mysql_query($select);
while($value = mysql_fetch_array($query1)){ ?>
<tr>
<td><?php echo $value[id];?></td>
<td><?php echo $value[company ];?></td>
<td><?php echo $value[type];?></td>
<!--<td> </td>-->
<?php /*?><td><?php echo $value[amount];?></td>
<td><?php echo $value[date];?></td><?php */?>
<td><i class="icon-edit"></i>
<i class="icon-trash"></i></td>
</tr><?php }?>
</tbody>
</table>
</fieldset>
<form>
</div>
</body>
so i just want to print the table not whole page.
Use css to hide elements you don't want to print:
#media print {
.control-group {
display: none;
}
}
function printContent(el){
var restorepage = document.body.innerHTML;
var printcontent = document.getElementById(el).innerHTML;
document.body.innerHTML = printcontent;
window.print();
document.body.innerHTML = restorepage;
}
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<h1>My page</h1>
<div id="div1">DIV 1 content...</div>
<button onclick="printContent('div1')">Print Content</button>
<div id="div2">DIV 2 content...</div>
<button onclick="printContent('div2')">Print Content</button>
<p id="p1">Paragraph 1 content...</p>
<button onclick="printContent('p1')">Print Content</button>
</body>
</html>
You could make a print-css (which does the same as #media print, but I think it is cleaner, and you can disable the css include via javascript, if you need it):
<link rel="stylesheet" type="text/css" href="print.css" media="print" />
In that css, you hide all elements which should not get printed, for example:
.wrapper, .header {display: none;}
One posible solution, perhaps not the best option:
1. Open a new window with JS
2. Copy the whole table into the new window (with jQuery for example)
3. Print the new window
4. Close the window
Sure it has a blink effect but It will work.
So, my formular is redirecting to the wrong site!
Look at my example:
ATM I've got the internet address:
http://localhost/myworkspace/mywebsite/index.php
It's hosted on a localhost-xampp server. It's homepage is:
http://localhost/xampp/
Ok ... Now I tried several things to get into the file where the script is that is working with the data of the formular:
<form action="index.php">
-> I'm getting redirected to the Xampp Homepage
<form action="index.php?mod=home">
-> I'm getting redirected to the Xampp Homepage
<form action="myworkspace/mywebsite/index.php">
-> I'm getting redirected on "http://localhost/myworkspace/mywebsite/myworkspace/mywebsite/index.php"
(the arrows express what happens when clicking on "send")
Why is this happening? As seen in the examples, the browser is not moving outside the myworkspace/mywebsite-folders. But still, it loads the index.php which is located in localhost/xampp (it's localhost/index.php). If you see my original link of the website, there actually is an index.php file in "myworkspace/mywebsite".
Here's some code of me:
the addguestbook.php, which is included into the index.php (see below)
<?php
//******************************************************//
//********************Database stuff********************//
//******************************************************//
$host="localhost"; // Host name
$username="root"; // Mysql username
$password=""; // Mysql password
$db_name="mywebsite"; // Database name
//********************Tables***************************//
$tbl_name="guestbook"; // Guestbook
echo $hostname.$path.$get;
if ($_SERVER['REQUEST_METHOD'] == 'POST'){
$name = $_POST['name'];
$email = $_POST['email'];
$website = $_POST['website'];
$comment = $_POST['comment'];
$datetime = date("l, jS M Y, g:i a"); //date time
// Connect to server and select database.
mysql_connect($host, $username, $password)or die("cannot connect server: ".mysql_error());
mysql_select_db($db_name)or die("cannot select DB: ".mysql_error());
$sql="INSERT INTO ".$tbl_name."(id, name, email, website, comment, datetime)VALUES('".$post_id."', '".$name."', '".$email."', '".$website."', '".$comment."', '".$datetime."')";
$result=mysql_query($sql);
mysql_close();
} else
?>
<form method="post" action="myworkspace/mywebsite/index.php">
<table border="0" cellspacing="0" cellpadding="0">
<tr>
<td class="guestbookFormCell" colspan='2'><input
class="guestbookInputFieldText" name="name" type="text"
value="Name *" size="40" maxlength="30" /></td>
</tr>
<tr>
<td class="guestbookFormCell" colspan='2'><input
class="guestbookInputFieldText" name="email" type="text"
value="E-Mail (won't become displayed)" size="40" maxlength="40" />
</td>
</tr>
<tr>
<td class="guestbookFormCell" colspan='2'><input
class="guestbookInputFieldText" name="website" type="text"
value="Website" size="40" maxlength="50" /></td>
</tr>
<tr>
<td class="guestbookFormCell" colspan='2'><textarea
class="guestbookInputFieldText" name="comment" cols="37" rows="5">Comment *</textarea>
</td>
</tr>
<!--
<tr>
<td>CAPTCHA</td>
</tr>
-->
<tr>
<td><button class="guestbookFormCell guestbookButton" type="submit"
name="submit">
<span class='guestbookButtonText'>Send</span>
</button></td>
<td><button class="guestbookFormCell guestbookButton" style="float:right;"type="reset"
name="reset">
<span class='guestbookButtonText'>Reset</span>
</button></td>
</tr>
</table>
</form>
and the index.php (it's pretty big. most of it is just a design-structure, so ignore the html stuff. it's just divs and tables.
<html>
<head>
<title>JustBasti's website</title>
<script type="text/javascript" src="lightbox/js/prototype.js"></script>
<script type="text/javascript"
src="lightbox/js/scriptaculous.js?load=effects,builder"></script>
<script type="text/javascript" src="lightbox/js/lightbox.js"></script>
<link rel="stylesheet" href="lightbox/css/lightbox.css" type="text/css"
media="screen" />
<link rel="stylesheet" type="text/css" href="style.css" />
</head>
<body>
<?php
session_start();
// Path-parameters for link-building
$hostname = $_SERVER['HTTP_HOST'];
$path = $_SERVER['PHP_SELF'];
$_SESSION['basepath'] = dirname($_SERVER['PHP_SELF']);
?>
<div id='top'>
<div id='header'>
<div id='headerText' class='lightText text bigText'>
<div>
Hello, My name is <font class='blueText'>Sebastian Fast</font>
</div>
<div>
I just finished my <font class='blueText'>education in IT</font>
</div>
<div>
and am now <font class='blueText'>aiming to</font>
</div>
<div>
experience the <font class='blueText'>most wonderful places</font>
</div>
<div>
around <font class='blueText'>the world</font>
</div>
<div>
Read more about myself <font class='blueText'>here</font>
</div>
</div>
<div id='headerLineBox'>
<div class='lineTopDiv'>
<div id='headerTopLine' class='lineTopEnd'></div>
</div>
<div class='verticalLine'>
<div id='headerLine' class='line'></div>
</div>
<div class='lineBottomDiv'>
<div class='lineBottomEnd'></div>
</div>
</div>
<div id='headerNavigation' class='lightText text bigText'>
<div>
<a href='index.php?mod=home' class='lightLink'>Home</a>
</div>
<div>
<a href='index.php?mod=news' class='lightLink'>News</a>
</div>
<div>
<a href='index.php?mod=countries' class='lightLink'>Countries</a>
</div>
<div>
<a href='index.php?mod=gallery' class='lightLink'>Gallery</a>
</div>
</div>
</div>
</div>
<div id='middle'>
<div id='content'>
<table>
<!-- CONTENT -->
<?
// Paths
echo " <a href='index.php?mod=home'>Home</a>
<a href='index.php?mod=news'>News</a>
<a href='index.php?mod=allnews'>All News</a>
<a href='index.php?mod=countries'>Countries</a>
<a href='index.php?mod=gallery'>Gallery</a>
<a href='index.php?mod=guestbook'>Guestbook</a>
<a href='index.php?mod=admin'>Administrator</a>";
if (!isset($_SESSION['angemeldet']) || !$_SESSION['angemeldet']) {
include('login/login.php');
}
// Check which link got clicked & import data
if (!$_GET){
include('data/home.php');
} elseif (isset($_GET['mod']) && !isset($_GET['post']) && !isset($_GET['album']) && !isset($_GET['country'])){
Switch($_GET['mod']){
case 'home':
include('data/home.php');
exit;
case 'news':
include('data/latestPosts.php');
exit;
case 'allnews':
include('data/allPosts.php');
exit;
case 'countries':
include('data/viewCountries.php');
exit;
case 'gallery':
include('data/gallery.php');
exit;
case 'admin':
include('admin/admin.php');
exit;
}
} elseif (isset($_GET['mod']) && isset($_GET['post'])){
$get = "?mod=".$_GET['mod']."&post=".$_GET['post'];
$post_id = $_GET['post'];
include('data/viewPost.php');
include('data/guestbook.php');
} elseif (isset($_GET['mod']) && isset($_GET['album'])){
$get = "?mod=".$_GET['mod']."&album=".$_GET['album'];
$post_id = $_GET['album'];
include('data/viewAlbum.php');
include('data/guestbook.php');
} elseif (isset($_GET['mod']) && isset($_GET['country'])){
$country = $_GET['country'];
include('data/viewCountry.php');
}
?>
</table>
</div>
</div>
<div id='graphicDIV'>
<div id='graphic'></div>
</div>
<div id='bottom'>
<div id='guestbook'>
<table border='0' cellspacing='0' cellpadding='0'>
<tr>
<td id='guestbookHeadline1'><font class='lightText bigText text'>Allgemeines Gästebuch</font>
</td>
<td id='guestbookTopLine' class='lineTopEnd'></td>
<td id='guestbookHeadline2'><font class='lightText bigText text'>Schreibe selbst</font>
</td>
</tr>
<tr id='guestbook2'>
<?
$post_id = 000000000000;
$get = "?mod=home";
?>
<td id='guestbookComment'>
<? include('data/viewguestbook.php'); ?>
</td>
<td id='guestbookLine'></td>
<td id='guestbookFormular'>
<? include('data/addguestbook.php'); ?>
</td>
</tr>
</table>
</div>
<div id='footerLeftLine' class='lineLeftEnd'>
<!-- LineLeftEnd -->
</div>
<div id='footerLine' class='horizontalLine'>
<!-- Line -->
</div>
<div id='footerRightLine' class='lineRightEnd'>
<!-- LineRightEnd -->
</div>
<div id='footer'>
<div class='footerLink'>
<!-- AdministratorLink -->
</div>
<div class='footerLink'>
<!-- ImpressumLink -->
</div>
<div class='footerLink'>
<!-- NewsArchiveLink -->
</div>
</div>
</div>
</body>
</head>
</html>
Thanks for help!
<form action="myworkspace/mywebsite/index.php">
should be
<form action="/myworkspace/mywebsite/index.php">
What you currently have is a path relative to your current page. The path that I have given is relative to your document root, which is http://localhost.
See also:
Absolute and Relative Paths, from About.com
Path (computing), from Wikipedia