<?php
if (!isset($_POST['submit1'])==($submit == "Dig Gmail")){
if (!function_exists("ssh2_connect"))
die("function ssh2_connect doesn't exist");
$connection = ssh2_connect('x.x.x.x', 22);
ssh2_auth_password($connection, 'xxxx', 'xxxx');
$stream = ssh2_exec($connection, 'dig #127.0.0.1 gmail.com');
$errorStream = ssh2_fetch_stream($stream, SSH2_STREAM_STDERR);
if (stream_set_blocking($stream, true)) {
echo "<pre>";
echo "Output:".stream_get_contents($stream);
echo "</pre>";
} elseif (stream_set_blocking($errorStream, true)) {
echo "Error:".stream_get_contents($errorStream);
} else {
echo "No Output";
} fclose($errorStream);
fclose($stream);
}
?>
<form action="" method="post">
<input type="submit" class="btn btn-success"
name="submit1" value="Dig Gmail">
</form>
the message for error code is
Notice: Undefined variable: submit in C:\xampp\htdocs\coba\home.php on line 402
thanks for your advice
You are using POST variables from your form below, therefore you should use the $_POST array, containing the variables submitted from the from.
The above example would work in some older PHP versions with insecure registre_globals turned on (which is never a good idea). Also the name was wrong.
Modify the line
if (!isset($_POST['submit1'])==($submit == "Dig Gmail")){
to
if (!isset($_POST['submit1'])==($_POST['submit1'] == "Dig Gmail")){
and it should do the job for you.
Related
i'm currently working on a small script for my Homepage but i ran into a problem.
I Try to upload an Image, but it seems like the POST data from the form is not being received. What did i do wrong?
I already changed the post_max_size and everything in the php.ini.
These are the Errors i get:
"Notice: Undefined index: image in ...." & "Notice: Undefined index:
submit in ...."
<form method="POST" action="/eye/sites/handling/post.php" enctype="multipart/form-data">
<div class="fileUpload">
<span><i class="fa fa-folder-o" aria-hidden="true"></i> Bild wählen</span>
<input type="file" class="upload" name="image"/>
</div>
<input type="submit" value="Upload It!" name="submit"/>
</form>
<?php session_start();
error_reporting(E_ALL);
if (isset($_SESSION["login_stat"])) {
date_default_timezone_set('Europe/Berlin');
$config = "$_SERVER[DOCUMENT_ROOT]/eye/more/config.xml";
$xml = simplexml_load_file($config);
$picWidth = $xml->pic->width;
$picHeight = $xml->pic->height;
$fulldate = date('dmYHis');
if(isset($_POST["submit"])) {
if (file_exists($_FILES['image']['tmp_name']) || is_uploaded_file($_FILES['image']['tmp_name'])) {
$typeCheck = $_FILES['image']['type'];
if ($typeCheck != "image/jpeg") {
$error = "Not a .jpg";
header('location: /eye/sites/post.php?stat=bad&error='.$error);
exit;
}
$file = $_SERVER['DOCUMENT_ROOT']."/uploads/".$fulldate.".jpg";
$type = "image/jpeg";
move_uploaded_file($_FILES['image']['tmp_name'], $file);
$file_thmb = $_SERVER['DOCUMENT_ROOT']."/uploads/!1A_thmb/".$fulldate.".jpg";
include "resize-class.php";
$resizeObj = new resize($file);
$resizeObj->resizeImage($picWidth, $picHeight, 'crop');
$resizeObj->saveImage($file_thmb, 100);
// header('location: /eye/sites/post.php?stat=good');
} else{
// header('location: /eye/sites/post.php?stat=bad&error=No File');
}
} else{
// header('location: /eye/sites/post.php?stat=bad&error=No Data');
echo $_SERVER['CONTENT_TYPE'];
echo "<br>";
echo $_FILES['image']['tmp_name'];
echo "<br>";
echo $_POST['submit'];
echo "<br>";
}
} else {
header('location: /eye/index.php?stat=in');
}
?>
Edit:
The problem is definitely about my Localhost.
This whole thing is working fine on my Webspace, but on my localhost it's not working.
BUT: I'm not getting errors anymore, when is click on Submit it goes to the php file that should save the image, but nothing is happening. I just see a white Page.
But like i said, it runs perfectly on my webspace..
If this is running on your local machine, do a quick check to make sure your "php.ini" file is configured to allow file uploads.
php.ini
file_uploads = On
The codes look fine. Check if your form action is posting to the correct path and if I may suggest using a simpler approach to test your file upload function before making it more complex. Use the following to start testing.
if (isset($_POST["submit"])) {
if (file_exists($_FILES['image']['tmp_name']) || is_uploaded_file($_FILES['image']['tmp_name'])) {
echo "Upload is working!";
}
}
Keep us updated on your findings.
Perhaps this general information will help someone, as it helped me: a submitted form will only include fields that have defined 'name' attributes. 'id' is not enough.
The idea is that 'id' identifies an element in the DOM for use by JavaScript (either as a global variable or for use in document.getElementById(ID)), but 'name' identifies those elements whose names and values will be sent to the destination ('action') page.
So it makes sense that there are two different identifying attributes, used in two different ways.
I would like to send custom text by web to my game server throught this script, but i dunno how to define the text inside input
Any tip or suggestion or fix?
the script is:
<?php
function send_command($ip,$port,$rcon,$command) {
$fp = #fsockopen("udp://".$ip, $port, $errno, $errstr);
if ($fp){
$request = chr(1).chr(0).chr(242).chr(strlen($rcon)).$rcon.pack("S",strlen($command)).$command;
fwrite($fp, $request);
}
}
$name = "Owner";
echo "<input type='text' name='$msg'";
if (isset($_REQUEST['send'])) {
send_command("192.168.1.69",36963,"asdasd","say ©255255255".$name.": ".$msg);
}
?>
<html>
<form>
<input type="submit" value="send" name="send">
</form>
</html>
"Error "is:
Notice: Undefined variable: msg in D:\xampp\htdocs\test\index.php on line 11
Notice: Undefined variable: msg in D:\xampp\htdocs\test\index.php on line 14
Don't define your input's ID with a '$'. PHP interprets that as a variable. Name it 'msg'. You may then reference it with $_POST['msg'] or $_GET['msg'], depending on how you sent it to the PHP file.
I have written a code - It takes user input and displays sha1() string for the input.
I get the 'undefined index' notice on local host even after declaring the variables and using isset() function.
Notice: Undefined index: enc in C:\Program Files\EasyPHP-12.1\www\MySQL\localhost_drupal\encrypt.php on line 61
It works fine on the remote (production) server.
What is the fault in the following code?
<?php
//variable declaration
$enc = $len = "";
// checking with isset
if(isset($_POST['enc'])) {
//sha1()
$enc = sha1($_POST['enc']);
// strlen function
$len = strlen($_POST['enc']);
}
?>
//HTML
<!DOCTYPE html>
<html>
<head><title>Generate Password</title></head>
<body>
<form name = "encry" action = "encrypt.php" method = "post">
<input type = "text" name = "enc" >
<input type = "submit" value = "Submit">
</form>
//OUTPUT
<?php
//Line 61
echo "<p> You entered :" . $_POST['enc']."</p>";
echo "<p> Length of the string you entered :" . $len. "<p>";
echo "<p> Your Password is : ". $enc . "</p>";
?>
</body>
</html>
The reason being for the error is that your entire code is inside one page and errors out on initial load. Error reporting is set on one server, but isn't on the other.
You should be using isset() against a named submit button (which I have named) and around where you're using
echo "<p> You entered :" . $_POST['enc']."</p>"; etc..
Change your code to the following:
<?php
//variable declaration
$enc = $len = "";
// checking with isset
if(isset($_POST['enc'])) {
//sha1()
$enc = sha1($_POST['enc']);
// strlen function
$len = strlen($_POST['enc']);
}
?>
//HTML
<!DOCTYPE html>
<html>
<head><title>Generate Password</title></head>
<body>
<form name = "encry" action = "encrypt.php" method = "post">
<input type = "text" name = "enc" >
<input type = "submit" value = "Submit" name = "submit">
</form>
//OUTPUT
<?php
if(isset($_POST['submit'])) {
//Line 61
echo "<p> You entered :" . $_POST['enc']."</p>";
echo "<p> Length of the string you entered :" . $len. "<p>";
echo "<p> Your Password is : ". $enc . "</p>";
}
?>
</body>
</html>
To set error reporting on:
Add error reporting to the top of your file(s) which will help find errors.
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
// rest of your code
Sidenote: Error reporting should only be done in staging, and never production.
It's an E_NOTICE. Likely on production you suppress error messages (or at least notices). This is fine, but it does explain why it breaks in development.
Fix the issue and the notice will go away.
I am new to php. I am writing a php script which will take only c, cpp and java files and upload them to a server.
Here is my code:
<?php
if(!empty($_POST["file_submit"]))
{
$allowed_xtensions = array("c","cpp","java");
$tmp_xtension_array = explode(".",$_FILES["file_name"]["name"]);
$xtension = end($tmp_xtension_array);
$type = $_FILES["file_name"]["type"];
if((($type == "text/x-java-source") || ($type == "text/x-csrc") || ($type == "text/x-c")) && in_array($xtension,$xtensions))
{
if($_FILES["file"]["error"] > 0)
{
exit( "Unable to process file.\nError : ".$_FILES["file_name"]["error"]);
}
else
{
if(!file_exists("/code/test.",$xtension))
{
if(move_uploaded_file($_FILES["file_name"]["tmp_name"],"/code/test.",$xtension) == false)
{
exit("Unable to process the file.\n");
}
}
}
}
else
{
exit("Invalid File.");
}
}
else
{
$file_form = ' <html>
<body>
<form action="test.php" method="post" enctype="multipart/form-data">
<label for="file"> Select File : </label>
<input type="file" name="file_name" id="file_id"/><br />
<input type="submit" name="file_submit" value="Submit">
</form>
</body>
</html>';
echo $file_form;
}
?>
But whenever I upload c,cpp or java files, I get INVALID FILE in response, although I have uploaded the correct file. Can anyone tell me why my code is showing this behaviour?
shouldn't that be
... && in_array($xtension,$allowed_xtensions) ...
instead of
.... && in_array($xtension,$xtensions) ...
Otherwise, second parameter isn't an array and you might get an error on that and it's always false?
It appears that your variable $type is not being set.
As a test, you might want to stick in this statement near the beginning of your script:
print_r($_FILES);
I suspect that there is no key ['file']['type'] in what's being passed to your script (or at least the values aren't what you're expecting). You can also test for existence using isset($_FILES['file']['type']).
It is also possible that $_FILES is empty. If it is, then try adding enctype="multipart/form-data" to the form tag and make sure file uploads are enabled.
Code listed below is working fine for me, but the problem is that if I kept user & password
blank, and press login button then I am getting below error code:
Notice: Undefined index: in C:\xampp\htdocs\login.php on line 6
Even if I kept wrong user id & password then I get below error code:
Notice: Undefined index: r in C:\xampp\htdocs\login.php on line 6
Incorrect username/password. Please, try again.
Here “Incorrect username/password. Please, try again.” Is absolutely right information
I need all your helping hand to resolve unnecessary error code:
<?php
session_start();
include("passwords.php");
if(isset($_POST['ac'])) {
if ($_POST["ac"]=="log") {
if ($USERS[$_POST["username"]]==$_POST["password"]) {
$_SESSION["logged"]=$_POST["username"];
} else {
echo 'Incorrect username/password. Please, try again.';
};
};
}
if (array_key_exists($_SESSION["logged"],$USERS)) {
echo "You are logged in.";
header('Location: /dip01.php');
} else {
echo '<form action="login.php" method="post"><input type="hidden" name="ac"
value="log"> ';
echo 'Username: <input type="text" name="username" />';
echo 'Password: <input type="password" name="password" />';
echo '<input type="submit" value="Login" />';
echo '</form>';
};
?>
$USERS[$_POST["username"]]
when you leave the username input parameter blank, then it'll evaluate to:
$USERS[""]
what is a key that doesn't exist:
check for:
if(isset($_POST['ac'], $_POST["username"], $USERS[$_POST["username"]], $_POST["password"])) { /* ... */ }
instead of only checking for if sent:
if(isset($_POST['ac'])) { /* ... */ }
This error comes from the fact that you're using the user input $_POST['username'] as a key into the array $USERS without checking first that the key exists.
You could use isset to check that the key exists before accessing it, like this:
if (isset ($USERS[$_POST['username']]) && $USERS[$_POST['username']] == $_POST['password'])...