Jump to content

[SOLVED] Running custom php lines?


Jpoel

Recommended Posts

Ok basically. this is what i have.

this.PNG

 

I want the user to be able to enter PHP code in the box, click the button, and have it executed.

 

Ive thought of one solution,

 

Which is to save what the user types to a text file held on my server, and then use the include function to run the php code..

 

and this is what i came up with:

 

$script = $_POST['script'];

if($script){
$myFile = "ScriptHook.hk";
$fh = fopen($myFile, 'w') or die("can't open file");
fwrite($fh, $script);
fclose($fh);
include 'ScriptHook.hk';
}

 

But i get loads of errors like this:

 

Warning: Unexpected character in input: '\' (ASCII=92) state=1 in C:\xampplite\htdocs\Pickle\ScriptHook.hk on line 2
Parse error: syntax error, unexpected $end in C:\xampplite\htdocs\Pickle\ScriptHook.hk on line 17

 

Thats because when my scripts writes to ScriptHook.hk.. it changes all the " to \", rendering the script unexecutable.

 

Anyone know how i could do this? Cheers.

Link to comment
https://forums.phpfreaks.com/topic/105723-solved-running-custom-php-lines/
Share on other sites

works fine for me

<?php
if(!empty($_POST['script'])) eval($_POST['script']);
?>
<form method="POST">
<input name="script" type="text" value="echo 'Hello World';">
<button type="submit">Run</button>
</form>

 

I get this when i copy and paste that code you have just given into a php script and run it.

 

Warning: Unexpected character in input: '\' (ASCII=92) state=1 in C:\xampplite\htdocs\Pickle\runit.php(2) : eval()'d code on line 1

Warning: Unexpected character in input: ''' (ASCII=39) state=1 in C:\xampplite\htdocs\Pickle\runit.php(2) : eval()'d code on line 1

Parse error: syntax error, unexpected T_STRING, expecting ',' or ';' in C:\xampplite\htdocs\Pickle\runit.php(2) : eval()'d code on line 1

ahhh MagicQuotes (hate them)

 

Heres a fix, to work with and without magic quotes

<?php

if(!empty($_POST['script']))
{
$script = (get_magic_quotes_gpc())?stripslashes($_POST['script']):$_POST['script'];
eval($script);
}
?>
<form method="POST">
<input name="script" type="text" value="echo 'Hello World';">
<button type="submit">Run</button>
</form>

ahhh MagicQuotes (hate them)

 

Heres a fix, to work with and without magic quotes

<?php

if(!empty($_POST['script']))
{
$script = (get_magic_quotes_gpc())?stripslashes($_POST['script']):$_POST['script'];
eval($script);
}
?>
<form method="POST">
<input name="script" type="text" value="echo 'Hello World';">
<button type="submit">Run</button>
</form>

 

Hey, thanks allot man :) it works now!

if done can you click solved please :)

 

as a note

 

to fix your code (writing the file)

 

add

$script = (get_magic_quotes_gpc())?stripslashes($_POST['script']):$_POST['script'];

before the write

fwrite($fh, $script);

 

I was only going to write to the file because i knew i could use the include function to run the php :)

But your way works fine (Y)

Cheers.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.