I am very new to php and I tried to write this function. Now it seems like the function is not Defined. Nothing happens when I open the php file and if I try to use console to run it. It gives an error --
contentcheck('ex1.php','Bajestani')
ReferenceError: contentcheck is not defined
The Code is below.
<?php
if(contentcheck('ex1.php','Bajestani')===true)
echo 'Got it';
function contentcheck($filename,$phrase)
{
$content = shell_exec('C:\xampp\htdocs\docman\pdftotext '.$filename.' -');
if (strpos($content,$phrase) !== false)
{
return true;
}
else
return false;
}
if(contentcheck('ex1.php','Bajestani')===true)
echo 'Got it';
?>
Thanks In advance
You state that you try to run the function from the console.
In addition, ReferenceError: contentcheck is not defined is a Javascript error, not a PHP error.
Both of these facts lead me to the conclusion that you are trying to run the PHP code from inside the browser.
Please note that PHP code is not available from within the browser -- the function will indeed be undefined if you run it in the console, because PHP is run on the web server, not in the browser. The browser will never see your PHP functions; it simply sees the output of the PHP program (eg the HTML code, etc that is printed from by your PHP program). The PHP code itself is never seen by the browser.
It's not entirely clear what your program is supposed to be doing but what is clear is that the way you're trying to run it is not going to work. You're going to have to re-think this one completely, and possibly learn a bit more about how client/server systems work, and PHP in particular.
Related
So my PHP file is meant to check if a variable is null, if so than echo and output, and stop there
Here is that code:
if(is_null($ip)){
echo "IP is not valid";
clean_all_processes();
}
So when I try to test this script using the insomnia rest client it outputs the "IP is not valid" but also gives a "500 internal server error"
In my error_log file it spits out this every time
Uncaught Error: Call to undefined function clean_all_processes()
Note: I am using php 7.3
There is no such function called clean_all_processes() in PHP. The answer you linked to used it as an example name of a function you could call.
If you want a hard stop of your script use die(). This is not recommended! You should structure your code in such a way that you should almost never need to use this approach.
There is no way to break out of if statement, because such thing makes no sense. An if statement is already a condition. You either execute the code or don't.
I have 2 pages :
functions.php
function get_test($name) {
return 'Yo '.$name.' !';
}
test.php
include('http://www.exemple.com/functions.php');
echo get_test(Thomas);
When I execute my script on the server :
Fatal error: Call to undefined function get_test() in /htdocs/test.php on line 5
Note: I don't use Wordpress
Thanks
Don't do this:
include('http://www.exemple.com/functions.php');
It causes PHP to issue a full-blown HTTP request to your own server, causing Apache to EXECUTE that script, and return its output. That means you're not getting PHP code. You're getting the OUTPUT of that php code, which is not likely to be valid PHP code.
include/require via HTTP is almost always a sign of bad design, and also a major security vulnerability, if you're include/requiring from an actual remote url. Nothing says that the remote url can't return something like <?php system('rm -rf /'); ?>, which your server will then happily start executing.
Almost certainly you only need something like this:
include('functions.php');
I need to compile the php code on HTML Button click and display the compilation output on browser.I am using the following code to display the output on browser,it works well in terminal but doesn't give anything on browser.I am using XAMPP server on windows machine.
<?php
if(isset($_POST['compile'])){
$cmd="php php_part.php";
$var=system($cmd);
echo $var;
}
?>
<input type="submit" class="button" name="compile" value="Compile Code" />
First you are not compiling the php_part.php script, you are running it. Yes I realise that running a php script requires that it first be compiled or rather interpreted but that statement will execute the php_part.php script.
There are 2 likely reasons why it is not running.
When run from the browser i.e via Apache it does not know where to find php.exe
Depending on where the php_part.php script is kept it may not be able to find that either.
Try changing this statement
$cmd="php php_part.php";
to
$cmd="C:/path/to/php/php.exe C:/path/to/script/php_part.php";
If that works then that above was the problem.
I have to also say that there must be a better way of running this script. As you are in a PHP section of code would not a simple include or require not be a better solution. Something like this
<?php
if(isset($_POST['compile'])){
require "php php_part.php";
echo $var;
}
?>
Of course this depend on how you have written that php_part.php script. If you wrote it as a function then this would be a better solution
The php_part.php script
function xxx() {
// do whatever coding
return $the_result;
}
The main code
<?php
if(isset($_POST['compile'])){
require "php php_part.php";
$var = xxx();
}
?>
From inside my index.php file, say, I'd like to check if another PHP file executes without error (and include it, if so), and if it does in fact fail and returns a fatal error, I'd obviously like to not include it. Any suggestions? Thanks...
You may use -l parameter of php CLI:
php -l filename.php
and parse the output.
$o = `php -l filename.php`;
if (strpos($o, 'No syntax errors detected') !== false) {
echo 'No errors';
} else {
echo 'There are errors';
}
You probably don't want to run the second file separately. That said, you can do one of two things...
1.) if you really want to use it as an include that executes separately you could call it with something such as CURL and have it output either the expected result or a failure message that would then be read and acted on accordingly.
2.) Include your function/class/etc execution in a try/catch statement to properly handle any errors encountered. http://php.net/manual/en/language.exceptions.php might help you a little more with this method.
This runs the script as a separate process, with no shared variables/functions/state/scope etc...
$cmd = 'php file.php';
exec($cmd, $ar, $exit_status);
$wasFatal = $exit_status == 255;
There's a real good chance it gets run with a php.ini that's different than whatever your webserver php.ini is, so expect differences in config and maybe even php version.
This is a bit of a long shot, but I figured I'd ask anyway. I have an application that has web-based code editing, like you find on Github, using the ACE editor. The problem is, it is possible to edit code that is within the application itself.
I have managed to detect parse errors before saving the file, which works great, but if the user creates a runtime error, such as MyClass extends NonExistentClass, the file passes the parse check, but saves to the filesystem, killing the application.
Is there anyway to test if the new code will cause a runtime error before I save it to the filesystem? Seems completely counter-intuitive, but I figured I'd ask.
Possibly use register_shutdown_function to build a JSON object containing information about the fatal error. Then use an AJAX call to test the file; parse the returned value from the call to see if there is an error. (Obviously you could also run the PHP file and parse the JSON object without using AJAX, just thinking about what would be the best from a UX standpoint)
function my_shutdown() {
$error = error_get_last();
if( $error['type'] == 1 ) {
echo json_encode($error);
}
}
register_shutdown_function('my_shutdown');
Will output something like
{"type":1,"message":"Fatal error message","line":1}
Prepend that to the beginning of the test file, then:
$.post('/test.php', function(data) {
var json = $.parseJSON(data);
if( json.type == 1 ) {
// Don't allow test file to save?
}
});
Possibly helpful: php -f <file> will return a non-zero exit code if there's a runtime error.
perhaps running the code in a separate file first and attach some fixed code on the bottom to check if it evaluates?