search
HomeBackend DevelopmentPHP TutorialHow to use the php single file version online code editor

这篇文章主要介绍了php单文件版在线代码编辑器,个人感觉相当不错,分享给大家,需要的朋友可以参考下

密码加密方式:
 * md5(自设密码+$ace) //$ace为cdn镜像地址

使用方法:

 * 1.确认 $pwd 变量值为 false, 上传本文件到PHP空间并访问
 * 2.第一次访问提示设置密码,设置密码并牢记
 * 3.使用第一次设置的密码登录后,默认编辑的是本php文件,
 * 4.本文件是编辑器核心文件,请不要随意修改
 * 5.保存编辑的文件请用 Ctrl + S 按键组合,等待执行结果
 * 6.保存动作执行后请务必等待保存成功信息返回
 * 7.重置操作会修改本程序的文件名,以防他人猜测路径
 * 8.刷新功能仅是刷新本程序文件,不能刷新其他

建议在 chrome 浏览器中使用本编辑器

代码如下:

<?php
session_start();
$curr_file = __FILE__; //默认编辑当前文件
$curr_file_path = str_replace(dirname(__FILE__), &#39;&#39;, __FILE__);
$pwd = "57574d98bc6ebe77b07e59d87065cd9e"; //密码初始化默认值为 false
$ace = &#39;ace.js&#39;; //编辑器核心js
$tip[&#39;core&#39;] = &#39;alertify.core.min.css&#39;;
$tip[&#39;css&#39;] = &#39;alertify.default.min.css&#39;;
$tip[&#39;js&#39;] = &#39;alertify.min.js&#39;;
$jquery = &#39;jquery.min.js&#39;;
if ( false !== $pwd ) {
    define(&#39;DEFAULT_PWD&#39;, $pwd);
}
//文件后缀名对应的语法解析器
$lng = array(
    &#39;as&#39; => &#39;actionscript&#39;, &#39;js&#39; => &#39;javascript&#39;,
    &#39;php&#39; => &#39;php&#39;, &#39;css&#39; => &#39;css&#39;, &#39;html&#39; => &#39;html&#39;,
    &#39;htm&#39; => &#39;html&#39;, &#39;ini&#39; => &#39;ini&#39;, &#39;json&#39; => &#39;json&#39;,
    &#39;jsp&#39; => &#39;jsp&#39;, &#39;txt&#39; => &#39;text&#39;, &#39;sql&#39; => &#39;mysql&#39;,
    &#39;xml&#39; => &#39;xml&#39;, &#39;yaml&#39; => &#39;yaml&#39;, &#39;py&#39; => &#39;python&#39;,
    &#39;md&#39; => &#39;markdown&#39;, &#39;htaccess&#39; => &#39;apache_conf&#39;,
    &#39;bat&#39; => &#39;batchfile&#39;, &#39;go&#39; => &#39;golang&#39;,
);
//判断用户是否登录
function is_logged() {
    $flag = false;
    if ( isset($_SESSION[&#39;pwd&#39;]) && defined(&#39;DEFAULT_PWD&#39;) ) {
        if ( $_SESSION[&#39;pwd&#39;] === DEFAULT_PWD ) {
            $flag = true;
        }
    }
    return $flag;
}
//重新载入到本页面
function reload() {
    $file = pathinfo(__FILE__, PATHINFO_BASENAME);
    die(header("Location: {$file}"));
}
//判断请求是否是ajax请求
function is_ajax() {
    $flag = false;
    if ( isset($_SERVER[&#39;HTTP_X_REQUESTED_WITH&#39;]) ) {
        $flag = strtolower($_SERVER[&#39;HTTP_X_REQUESTED_WITH&#39;]) === &#39;xmlhttprequest&#39;;
    }
    return $flag;
}
//销毁SESSION和COOKIE
function exterminate() {
    $_SESSION = array();
    foreach ( $_COOKIE as $key ) {
        setcookie($key, null);
    }
    session_destroy();
    $_COOKIE = array();
    return true;
}
//获取一个目录下的文件列表
function list_dir($path, $type = &#39;array&#39;) {
    $flag = false;
    $lst = array(&#39;dir&#39;=>array(), &#39;file&#39;=>array());
    $base = !is_dir($path) ? dirname($path) : $path;
    $tmp = scandir($base);
    foreach ( $tmp as $k=>$v ) {
        //过滤掉上级目录,本级目录和程序自身文件名
        if ( !in_array($v, array(&#39;.&#39;, &#39;..&#39;)) ) {
            $file = $full_path = rtrim($base, &#39;/&#39;).DIRECTORY_SEPARATOR.$v;
            if ( $full_path == __FILE__ ) {
                continue; //屏蔽自身文件不在列表出现
            }
            $file = str_replace(dirname(__FILE__), &#39;&#39;, $file);
            $file = str_replace("\\", &#39;/&#39;, $file); //过滤win下的路径
            $file = str_replace(&#39;//&#39;, &#39;/&#39;, $file); //过滤双斜杠
            if ( is_dir($full_path) ) {
                if ( &#39;html&#39; === $type ) {
                    $v = &#39;<li class="dir" path="&#39;.$file
                    .&#39;" onclick="load();"><span>&#39;.$v.&#39;</span></li>&#39;;
                }
                array_push($lst[&#39;dir&#39;], $v);
            } else {
                if ( &#39;html&#39; === $type ) {
                    $v = &#39;<li class="file" path="&#39;.$file
                    .&#39;" onclick="load()"><span>&#39;.$v.&#39;</span></li>&#39;;
                }
                array_push($lst[&#39;file&#39;], $v);
            }
        }
    }
    $lst = array_merge($lst[&#39;dir&#39;], $lst[&#39;file&#39;]);
    $lst = array_filter($lst);
    $flag = $lst;
    if ( &#39;html&#39; === $type ) {
        $flag = &#39;<ul>&#39;. implode(&#39;&#39;, $lst) .&#39;</ul>&#39;;
    }
    return $flag;
}
//递归删除一个非空目录
function deldir($dir) {
    $dh = opendir($dir);
    while ( $file = readdir($dh) ) {
        if ( $file != &#39;.&#39; && $file != &#39;..&#39; ) {
            $fullpath = $dir.&#39;/&#39;.$file;
            if ( !is_dir($fullpath) ) {
                unlink($fullpath);
            } else {
                deldir($fullpath);
            }
        }
    }
    return rmdir($dir);
}
//退出登录
if ( isset($_GET[&#39;logout&#39;]) ) {
    if ( exterminate() ) {
        reload();
    }
}
//ajax输出文件内容
if ( is_logged() && is_ajax() && isset($_POST[&#39;file&#39;]) ) {
    $file = dirname(__FILE__).$_POST[&#39;file&#39;];
    $ext = pathinfo($file, PATHINFO_EXTENSION);
    $mode = isset($lng[$ext]) ? $lng[$ext] : false;
    die(json_encode(array(
        &#39;file&#39; => $file, &#39;html&#39; => file_get_contents($file),
        &#39;mode&#39; => $mode,     
    )));
}
//ajax输出目录列表
if ( is_logged() && is_ajax() && isset($_POST[&#39;dir&#39;]) ) {
    $dir = dirname(__FILE__).$_POST[&#39;dir&#39;];
    $list_dir = list_dir($dir, &#39;html&#39;);
    die(json_encode(array(
        &#39;dir&#39; => $dir, &#39;html&#39; => $list_dir,
    )));
}
//ajax保存文件
if ( is_logged() && is_ajax() && isset($_POST[&#39;action&#39;]) ) {
    $arr = array(&#39;result&#39;=>&#39;error&#39;, &#39;msg&#39;=>&#39;文件保存失败!&#39;);
    $content = $_POST[&#39;content&#39;];
    if ( &#39;save_file&#39; === $_POST[&#39;action&#39;] ) {
        if ( isset($_POST[&#39;file_path&#39;]) ) {
            $file = dirname(__FILE__).$_POST[&#39;file_path&#39;];
        } else {
            $file = __FILE__;
        }
        file_put_contents($file, $content);
        $arr[&#39;result&#39;] = &#39;success&#39;;
        $arr[&#39;msg&#39;] = &#39;保存成功!&#39;;
    }
    die(json_encode($arr));
}
//ajax删除文件或文件夹
if ( is_logged() && is_ajax() && isset($_POST[&#39;del&#39;]) ) {
    $path = dirname(__FILE__).$_POST[&#39;del&#39;];
    $arr = array(&#39;result&#39;=>&#39;error&#39;, &#39;msg&#39;=>&#39;删除操作失败!&#39;);
    if ( $_POST[&#39;del&#39;] && $path ) {
        $flag = is_dir($path) ? deldir($path) : unlink($path);
        if ( $flag ) {
           $arr[&#39;msg&#39;] = &#39;删除操作成功!&#39;;
           $arr[&#39;result&#39;] = &#39;success&#39;;
        }
    }
    die(json_encode($arr));
}
//ajax新建文件或文件夹
if ( is_logged() && is_ajax() && isset($_POST[&#39;create&#39;]) ) {
    $flag = false;
    $arr = array(&#39;result&#39;=>&#39;error&#39;, &#39;msg&#39;=>&#39;操作失败!&#39;);
    if ( isset($_POST[&#39;target&#39;]) ) {
        $target = dirname(__FILE__).$_POST[&#39;target&#39;];
        $target = is_dir($target) ? $target : dirname($target);
    }
    if ( $_POST[&#39;create&#39;] && $target ) {
        $base_name = pathinfo($_POST[&#39;create&#39;], PATHINFO_BASENAME);
        $exp = explode(&#39;.&#39;, $base_name);
        $full_path = $target.&#39;/&#39;.$base_name;
        $new_path = str_replace(dirname(__FILE__), &#39;&#39;, $full_path);
        if ( count($exp) > 1 && isset($lng[array_pop($exp)]) ) {
            file_put_contents($full_path, &#39;&#39;);
            $arr[&#39;result&#39;] = &#39;success&#39;;
            $arr[&#39;msg&#39;] = &#39;新建文件成功!&#39;;
            $arr[&#39;type&#39;] = &#39;file&#39;;
        } else {
            mkdir($full_path, 0777, true);
            $arr[&#39;result&#39;] = &#39;success&#39;;
            $arr[&#39;msg&#39;] = &#39;新建目录成功!&#39;;
            $arr[&#39;type&#39;] = &#39;dir&#39;;
        }
        if ( $base_name && $new_path ) {
            $arr[&#39;new_name&#39;] = $base_name;
            $arr[&#39;new_path&#39;] = $new_path;
        }
    }
    die(json_encode($arr));
}
//ajax重命名文件或文件夹
if ( is_logged() && is_ajax() && isset($_POST[&#39;rename&#39;]) ) {
    $arr = array(&#39;result&#39;=>&#39;error&#39;, &#39;msg&#39;=>&#39;重命名操作失败!&#39;);
    if ( isset($_POST[&#39;target&#39;]) ) {
        $target = dirname(__FILE__).$_POST[&#39;target&#39;];
    }
    if ( $_POST[&#39;rename&#39;] ) {
        $base_name = pathinfo($_POST[&#39;rename&#39;], PATHINFO_BASENAME);
        if ( $base_name ) {
            $rename = dirname($target).&#39;/&#39;.$base_name;
            $new_path = str_replace(dirname(__FILE__), &#39;&#39;, $rename);
        }
    }
    if ( $rename && $target && rename($target, $rename) ) {
       $arr[&#39;new_name&#39;] = $base_name;
       $arr[&#39;new_path&#39;] = $new_path;
       $arr[&#39;msg&#39;] = &#39;重命名操作成功!&#39;;
       $arr[&#39;result&#39;] = &#39;success&#39;;
    }
    if ( $target == __FILE__ ) {
        $arr[&#39;redirect&#39;] = $new_path;
    }
    die(json_encode($arr));
}
//获取代码文件内容
$code = file_get_contents($curr_file);
$tree = &#39;<ul id="dir_tree">
    <li class="dir" path="/" onclick="load()">ROOT&#39;.list_dir($curr_file, &#39;html&#39;).&#39;</li>
</ul>&#39;;
//登陆和设置密码共用模版
$first = <<<HTMLSTR
<!DOCTYPE html>
<html><head><meta charset="UTF-8">
<title>【标题】</title>
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<style type="text/css" media="screen">
body {
    overflow: hidden; background-color: #2D2D2D; color: #CCCCCC; font-size: 12px; margin: 0;
    font-family: &#39;Monaco&#39;, &#39;Menlo&#39;, &#39;Ubuntu Mono&#39;, &#39;Consolas&#39;, &#39;source-code-pro&#39;, monospace;
}
form { display: none; position: absolute; }
form h5 { font-size: 14px; font-weight: normal; margin: 0; line-height: 2em; }
form input {
    color: #fff; border: 1px solid #369; border-radius: 3px; background: #333; height: 22px;
    line-height: 1.6em; width: 125px; margin-right: 5px; vertical-align: middle;
}
form button {
    line-height: 1.6em; border: 1px solid #369; border-radius: 3px;
    background: #369; color: #fff; vertical-align: middle;
}
</style>
<link rel="stylesheet" href="{$tip[&#39;core&#39;]}" />
<link rel="stylesheet" href="{$tip[&#39;css&#39;]}" />
</head>
<body>
    <form method="post">
        <input name="pwd" type="password" /><button type="submit">【动作】</button>
    </form>
    <script src="{$jquery}" type="text/javascript" charset="utf-8"></script>
    <script src="{$ace}" type="text/javascript" charset="utf-8"></script>
    <script src="{$tip[&#39;js&#39;]}" type="text/javascript"></script>
    <script type="text/javascript">
    var editor = false;
    $(function(){
        $(&#39;form&#39;).prepend(&#39;<h5 id="nbsp-document-title-nbsp">&#39;+ document.title +&#39;</h5>&#39;);
        $(&#39;form&#39;).css({
            left: ($(window).width()-$(&#39;form&#39;).width())/2,
            top: ($(window).height()-$(&#39;form&#39;).height())/2
        });
        $(&#39;form&#39;).show();
    });
    </script>
</body></html>
HTMLSTR;
//判断是否第一次登录
if ( false === $pwd && empty($_POST) ) {
    die(str_replace(
        array(&#39;【标题】&#39;, &#39;【动作】&#39;),
        array(&#39;第一次使用,请先设置密码!&#39;, &#39;设置&#39;),
        $first
    ));
}
//第一次设置登录密码
if ( false === $pwd && !empty($_POST) ) {
    if ( isset($_POST[&#39;pwd&#39;]) && strlen($_POST[&#39;pwd&#39;]) ) {
        $pwd = $_SESSION[&#39;pwd&#39;] = md5($_POST[&#39;pwd&#39;].$ace);
        $code = preg_replace(&#39;#\$pwd = false;#&#39;, &#39;$pwd = "&#39;.$pwd.&#39;";&#39;, $code, 1);
        file_put_contents($curr_file, $code);
    } else {
        reload();
    }
}
//用户登录验证
if ( false !== $pwd && !empty($_POST) ) {
    $tmp = md5($_POST[&#39;pwd&#39;].$ace);
    if ( $tmp && $pwd && $tmp === $pwd ) {
        $_SESSION[&#39;pwd&#39;] = $pwd;
        reload();
    }
}
//处理一下html实体
$code = htmlspecialchars($code);
$dir_icon = str_replace(array("\r\n", "\r", "\n"), &#39;&#39;,
&#39;data:image/jpg;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAANCAYAAACgu+4kAAAAGXRFWHRTb2Z0d2
FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAAQVJREFUeNqkkk1uwjAQhd84bsNP1FUXLCtu0H3XPSoX4Qrd9wR
sCjQEcIY3DiiJUYiqRhp5Mra/92YSUVVgLSW49B7H+NApRh75XkHfFoCG+02tyflUeQTw2y9UYYP8cCStc9SM
PeVA/Sy6Dw555q3au1z+EhBYk1cgO7OSNdaFNT0x5sCkYDha0WPiHZgVqPzLO+8seai6E2jed42bCL06tNyEH
AX9kv3jh3HqH7BctFWLMOmAbcg05mHK5+sQpd1HYijN47zcDUCShGEHtzxtwQS9WTcAQmJROrJDLXQB9s1Tu6
MtRED4bwsHLnUzxEeKac3+GeP6eo8yevhjC3F1qC4CDAAl3HwuyNAIdwAAAABJRU5ErkJggg==&#39;);
$file_icon = str_replace(array("\r\n", "\r", "\n"), &#39;&#39;,
&#39;data:image/jpg;base64,iVBORw0KGgoAAAANSUhEUgAAAA8AAAAQCAYAAADJViUEAAAAGXRFWHRTb2Z0d2
FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAAS1JREFUeNqMU01KxkAMTaez7aYbNwreQdBzeopS6EXEW+jug7Z
C6X+/iUloSr6xioFHJkPee5mUJgBwT7gjpPB3XAgfiBjs5dOyLF/btl0pkEFngdbzPGNRFK/U+0hwJAAMjmcm
DsOA4zge6Pseu67DpmlEqK5rLMvyRkDJor6uq2SGktu2FfdpmpANqqoSASYnO/kthABJkoCOxCASkCBkWSYuQ
qCeNE1fqHz3fMkXzjnJ2sRinL33QBNIzWJ5nh/L8npQohVTJwYTyfFm/d6Oo2HGE8ffwseuZ1PEjhrOutmsRF
0iC8QmPibEtT4hftrhHI95JqJT/HC2JOt0to+zN6MVsZ/oZKqwmyCTA33DkbN1sws0i+Pega6v0kd42H9JB/8
LJl5I6PNbgAEAa9MP7QWoNLoAAAAASUVORK5CYII=&#39;);
$loading = str_replace(array("\r\n", "\r", "\n"), &#39;&#39;,
&#39;data:image/gif;base64,R0lGODlhFAAUALMIAPh2AP+TMsZiALlcAKNOAOp4ANVqAP+PFv///wAAAAAAAA
AAAAAAAAAAAAAAAAAAACH/C05FVFNDQVBFMi4wAwEAAAAh+QQFCgAIACwAAAAAFAAUAAAEUxDJSau9iBDMteb
TMEjehgTBJYqkiaLWOlZvGs8WDO6UIPCHw8TnAwWDEuKPcxQml0Ynj2cwYACAS7VqwWItWyuiUJB4s2AxmWxG
g9bl6YQtl0cAACH5BAUKAAgALAEAAQASABIAAAROEMkpx6A4W5upENUmEQT2feFIltMJYivbvhnZ3Z1h4FMQI
Dodz+cL7nDEn5CH8DGZhcLtcMBEoxkqlXKVIgAAibbK9YLBYvLtHH5K0J0IACH5BAUKAAgALAEAAQASABIAAA
ROEMkphaA4W5upMdUmDQP2feFIltMJYivbvhnZ3V1R4BNBIDodz+cL7nDEn5CH8DGZAMAtEMBEoxkqlXKVIg4
HibbK9YLBYvLtHH5K0J0IACH5BAUKAAgALAEAAQASABIAAAROEMkpjaE4W5tpKdUmCQL2feFIltMJYivbvhnZ
3R0A4NMwIDodz+cL7nDEn5CH8DGZh8ONQMBEoxkqlXKVIgIBibbK9YLBYvLtHH5K0J0IACH5BAUKAAgALAEAA
QASABIAAAROEMkpS6E4W5spANUmGQb2feFIltMJYivbvhnZ3d1x4JMgIDodz+cL7nDEn5CH8DGZgcBtMMBEox
kqlXKVIggEibbK9YLBYvLtHH5K0J0IACH5BAUKAAgALAEAAQASABIAAAROEMkpAaA4W5vpOdUmFQX2feFIltM
JYivbvhnZ3V0Q4JNhIDodz+cL7nDEn5CH8DGZBMJNIMBEoxkqlXKVIgYDibbK9YLBYvLtHH5K0J0IACH5BAUK
AAgALAEAAQASABIAAAROEMkpz6E4W5tpCNUmAQD2feFIltMJYivbvhnZ3R1B4FNRIDodz+cL7nDEn5CH8DGZg
8HNYMBEoxkqlXKVIgQCibbK9YLBYvLtHH5K0J0IACH5BAkKAAgALAEAAQASABIAAAROEMkpQ6A4W5spIdUmHQ
f2feFIltMJYivbvhnZ3d0w4BMAIDodz+cL7nDEn5CH8DGZAsGtUMBEoxkqlXKVIgwGibbK9YLBYvLtHH5K0J0
IADs=&#39;);
//编辑器模版
$html = <<<HTMLSTR
<!DOCTYPE html>
<html><head><meta charset="UTF-8">
<title>ACE代码编辑器</title>
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<style type="text/css" media="screen">
a { text-decoration: none; }
body {
    overflow: hidden; background-color: #2D2D2D; font-size: 12px;
    font-family: &#39;Consolas&#39;, &#39;Monaco&#39;, &#39;Menlo&#39;, &#39;Ubuntu Mono&#39;, monospace;
    scrollbar-arrow-color: #ccc; scrollbar-base-color: #333;
    scrollbar-dark-shadow-color: #00ffff; scrollbar-track-color: #272822;
    scrollbar-highlight-color: #272822; scrollbar-3d-light-color: #272822;
    scrollbar-face-color: #2D2D2D; scrollbar-shadow-color: #333;
}
::-webkit-scrollbar { width:5px; height:6px; background-color:#444; }
::-webkit-scrollbar:hover { background-color:#444; }
::-webkit-scrollbar-thumb:hover { min-height:5px; min-width:5px; background-color: #AAA; }
::-webkit-scrollbar-thumb:active { -webkit-border-radius:20px; background-color: #AAA; }
::-webkit-scrollbar-thumb {
    min-height:5px; min-width:5px; -webkit-border-radius:20px; 
    ::-webkit-border-radius:1px; background-color: #AAA;
}
body > pre { color: #666; }
#sider { margin: 0; position: absolute; top:  25px; bottom: 0; left: 0; right: 85%; }
#editor { margin: 0; position: absolute; top: 0; bottom: 0; left: 15%; right: 0; }
#dir_tree { margin:0; padding: 0; height: 100%; overflow: auto; position: relative; left: 5px; } 
#dir_tree, #dir_tree ul, #dir_tree li { margin: 0; padding: 0; list-style: none inside; }
#dir_tree ul { padding-left: 20px; position: relative; }
#dir_tree li { text-indent: 2em; line-height: 1.6em; cursor: default; color: #ccc; }
#dir_tree li.hover > span, #dir_tree li:hover > span { color: #66D9EF; }
#dir_tree li#on > span { color: red; }
#dir_tree li.dir { background: url({$dir_icon}) no-repeat 3px 3px; }
#dir_tree li.file { background: url({$file_icon}) no-repeat 3px 0; }
#dir_tree li.loading { background: url({$loading}) no-repeat 3px 0; }
#logout { position: absolute; top: 0; left: 0; }
#logout a { display: inline-block; color: #aaa; line-height: 25px; padding: 0 4px; }
#logout a:hover { background: #000; color: #ddd; }
#contextmenu { position: absolute; top: 0; left: 0; background: #fff; color: #333; border: 1px solid #000; padding: 1px; }
#contextmenu span { display: block; line-height: 24px; text-indent: 20px; width: 80px; cursor: default; }
#contextmenu span:hover { background-color: #369; color: #fff; }
#alertify .alertify-message, #alertify .alertify-message {
    text-align: left !important; text-indent: 0; font-weight: bold; font-size: 16px;
}
#alertify .alertify-dialog, #alertify .alertify-dialog {
    font-family: &#39;Consolas&#39;; padding: 10px !important; color: #333 !important;
}
#alertify .alertify-button { 
    border-radius: 3px !important; font-weight: normal !important; 
    font-size: 14px !important; padding: 3px 15px !important;
}
.alertify-buttons { text-align: right !important; }
</style>
<link rel="stylesheet" href="{$tip[&#39;core&#39;]}" />
<link rel="stylesheet" href="{$tip[&#39;css&#39;]}" />
</head><body>
<p id="logout">
    <a href="javascript:void(0);">保存</a>
    <a href="javascript:void(0);">刷新</a>
    <a href="javascript:void(0);">重置</a>
    <a href="?logout">退出</a>
</p>
<p id="sider">{$tree}</p><pre id="editor">{$code}
<script></script> <script></script> <script></script> <script> var load = false; var curr_file = false; window.location.hash = &#39;&#39;; alertify.set({delay: 1000}); //n秒后自动消失 alertify.set({labels: {ok:&#39;确定&#39;,cancel:&#39;取消&#39;}}); var editor = false; $(function(){ //实例化代码编辑器 editor = ace.edit("editor"); //设置编辑器的语法和高亮 editor.setTheme("ace/theme/monokai"); editor.getSession().setMode("ace/mode/php"); //设置编辑器自动换行 editor.getSession().setWrapLimitRange(null, null); editor.getSession().setUseWrapMode(true); //不显示垂直衬线 editor.renderer.setShowPrintMargin(false); //editor.setReadOnly(true); //设置编辑器为只读 //editor.gotoLine(325); //跳转到指定行 //使编辑器获得输入焦点 editor.focus(); //绑定组合按键 var commands = editor.commands; commands.addCommand({ name: "save", bindKey: {win: "Ctrl-S", mac: "Command-S"}, exec: save_file }); //保存动作 function save_file() { if ( false == editor ) { return false; } var obj = { content: editor.getValue(), action: &#39;save_file&#39; }; if ( false !== curr_file ) { obj.file_path = curr_file; } alertify.log(&#39;正在保存...&#39;); $.post(window.location.href, obj, function(data){ if ( data.msg && &#39;success&#39; == data.result ) { alertify.success(data.msg); } else { alertify.error(data.msg); } }, &#39;json&#39;); } //加载目录列表或文件 load = function(ele) { var curr = $(event.srcElement); if ( ele ) { curr = ele; } if ( curr.is(&#39;span&#39;) ) { curr = curr.parent(&#39;li&#39;); } $(&#39;#dir_tree #on&#39;).removeAttr(&#39;id&#39;); curr.attr(&#39;id&#39;, &#39;on&#39;); var type = curr.attr(&#39;class&#39;); var path = curr.attr(&#39;path&#39;); window.location.hash = path; if ( &#39;file&#39; === type ) { alertify.log(&#39;正在加载...&#39;); curr.addClass(&#39;loading&#39;); $.post(window.location.href, {file:path}, function(data){ curr.removeClass(&#39;loading&#39;); if ( data.mode ) { editor.getSession().setMode("ace/mode/"+data.mode); } //注意,空文件应当允许编辑 if ( true || data.html ) { curr.attr(&#39;disabled&#39;, &#39;disabled&#39;); curr_file = path; //当前编辑的文件路径 //动态赋值编辑器中的内容 editor.session.doc.setValue(data.html); editor.renderer.scrollToRow(0); //滚动到第一行 editor.focus(); //编辑器获得焦点 setTimeout(function(){ editor.gotoLine(0); }, 800); } }, &#39;json&#39;); event.stopPropagation(); event.preventDefault(); return false; } if ( &#39;dir&#39; === type ) { if ( curr.attr(&#39;loaded&#39;) ) { curr.children(&#39;ul&#39;).toggle(); event.stopPropagation(); event.preventDefault(); return false; } else { curr.attr(&#39;loaded&#39;, &#39;yes&#39;); } alertify.log(&#39;正在加载...&#39;); curr.addClass(&#39;loading&#39;); $.post(window.location.href, {dir:path}, function(data){ curr.find(&#39;ul&#39;).remove(); curr.removeClass(&#39;loading&#39;); if ( data.html ) { curr.append(data.html); } }, &#39;json&#39;); } return false; } //绑定右键菜单 $(&#39;#sider&#39;).bind(&#39;contextmenu&#39;, function(e){ var path = false; var target = $(event.srcElement); if ( target.is(&#39;span&#39;) ) { target = target.parent(&#39;li&#39;); } if ( target.attr(&#39;path&#39;) ) { path = target.attr(&#39;path&#39;); } else { return false; } target.addClass(&#39;hover&#39;); var right_menu = $(&#39;#contextmenu&#39;); if ( !right_menu.get(0) ) { var timer = false; right_menu = $(&#39;<p id="contextmenu">&#39;); right_menu.hover(function(){ if ( timer ) { clearTimeout(timer); } }, function(){ timer = setTimeout(function(){ hide_menu(right_menu); }, 500); }); $(&#39;body&#39;).append(right_menu); } if ( path ) { right_menu.html(&#39;&#39;); var menu = $(&#39;<span>新建<span>浏览<span>重命名<span>删除&#39;); right_menu.append(menu); menu_area(right_menu, {left: e.pageX, top: e.pageY}); right_menu.find(&#39;span&#39;).click(function(){ switch ( $(this).text() ) { case &#39;新建&#39; : create_new(target, path); break; case &#39;浏览&#39; : preview(target, path); break; case &#39;重命名&#39; : re_name(target, path); break; case &#39;删除&#39; : del_file(target, path); break; } hide_menu(right_menu); }); } path ? right_menu.show() : hide_menu(right_menu); return false; }); //隐藏右键菜单 function hide_menu(menu) { $(&#39;#sider li.hover&#39;).removeClass(&#39;hover&#39;); if ( menu ) { menu.hide(); } } //右键菜单区域 function menu_area(menu, cfg) { if ( menu && cfg ) { var w = $(&#39;#sider&#39;).width() - menu.width(); var h = $(&#39;#sider&#39;).height() - menu.height(); if ( cfg.left > w ) { cfg.left = w; } if ( cfg.top > h ) { cfg.top = h; } menu.css(cfg); } } //保存按钮 $(&#39;#logout>a:contains("保存")&#39;).click(function(){ save_file(); return false; }); //刷新按钮 $(&#39;#logout>a:contains("刷新")&#39;).click(function(){ window.location.href = window.location.pathname; return false; }); //重置按钮 $(&#39;#logout>a:contains("重置")&#39;).click(function(){ alertify.confirm(&#39;是否修改 {$curr_file_path} 程序文件名?&#39;, function (e) { if ( !e ) { return &#39;cancel&#39;; } re_name($(&#39;<a>&#39;), &#39;{$curr_file_path}&#39;); }); return false; }); //新建操作 function create_new(obj, path) { if ( !obj || !path ) { return false; } alertify.prompt(&#39;请输入新建文件或文件夹名:&#39;, function (e, str) { if ( !e || !str ) { return false; } alertify.log(&#39;正在操作中...&#39;); $(&#39;#dir_tree #on&#39;).removeAttr(&#39;loaded&#39;).removeAttr(&#39;id&#39;); $.post(window.location.href, {create:str,target:path}, function(data){ if ( data.msg && &#39;success&#39; == data.result ) { alertify.success(data.msg); if ( obj.attr(&#39;class&#39;) == &#39;dir&#39; ) { load(obj); //重新加载子节点 } else { load(obj.parent().parent()); } } else { alertify.error(data.msg); } }, &#39;json&#39;); }); } //浏览操作 function preview(obj, path) { if ( !obj || !path ) { return false; } window.open(path, &#39;_blank&#39;); } //重命名 function re_name(obj, path) { if ( !obj || !path ) { return false; } alertify.prompt(&#39;重命名 &#39;+path+&#39; 为:&#39;, function (e, str) { if ( !e || !str ) { return false; } alertify.log(&#39;正在操作中...&#39;); $.post(window.location.href, {rename:str,target:path}, function(data){ if ( data.msg && &#39;success&#39; == data.result ) { alertify.success(data.msg); if ( data.redirect ) { window.location.href = data.redirect; } if ( data.new_name ) { obj.children(&#39;span&#39;).first().text(data.new_name); obj.attr(&#39;path&#39;, data.new_path); } } else { alertify.error(data.msg); } }, &#39;json&#39;); }); } //删除文件动作 function del_file(obj, path) { if ( !obj || !path ) { return false; } alertify.confirm(&#39;您确定要删除:&#39;+path+&#39; 吗?&#39;, function (e) { if ( !e ) { return &#39;cancel&#39;; } alertify.log(&#39;正在删除中...&#39;); $.post(window.location.href, {del:path}, function(data){ if ( data.msg && &#39;success&#39; == data.result ) { alertify.success(data.msg); obj.remove(); } else { alertify.error(data.msg); } }, &#39;json&#39;); }); } }); </script>

The above is the detailed content of How to use the php single file version online code editor. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
How does PHP type hinting work, including scalar types, return types, union types, and nullable types?How does PHP type hinting work, including scalar types, return types, union types, and nullable types?Apr 17, 2025 am 12:25 AM

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values ​​and handle functions that may return null values.

How does PHP handle object cloning (clone keyword) and the __clone magic method?How does PHP handle object cloning (clone keyword) and the __clone magic method?Apr 17, 2025 am 12:24 AM

In PHP, use the clone keyword to create a copy of the object and customize the cloning behavior through the \_\_clone magic method. 1. Use the clone keyword to make a shallow copy, cloning the object's properties but not the object's properties. 2. The \_\_clone method can deeply copy nested objects to avoid shallow copying problems. 3. Pay attention to avoid circular references and performance problems in cloning, and optimize cloning operations to improve efficiency.

PHP vs. Python: Use Cases and ApplicationsPHP vs. Python: Use Cases and ApplicationsApr 17, 2025 am 12:23 AM

PHP is suitable for web development and content management systems, and Python is suitable for data science, machine learning and automation scripts. 1.PHP performs well in building fast and scalable websites and applications and is commonly used in CMS such as WordPress. 2. Python has performed outstandingly in the fields of data science and machine learning, with rich libraries such as NumPy and TensorFlow.

Describe different HTTP caching headers (e.g., Cache-Control, ETag, Last-Modified).Describe different HTTP caching headers (e.g., Cache-Control, ETag, Last-Modified).Apr 17, 2025 am 12:22 AM

Key players in HTTP cache headers include Cache-Control, ETag, and Last-Modified. 1.Cache-Control is used to control caching policies. Example: Cache-Control:max-age=3600,public. 2. ETag verifies resource changes through unique identifiers, example: ETag: "686897696a7c876b7e". 3.Last-Modified indicates the resource's last modification time, example: Last-Modified:Wed,21Oct201507:28:00GMT.

Explain secure password hashing in PHP (e.g., password_hash, password_verify). Why not use MD5 or SHA1?Explain secure password hashing in PHP (e.g., password_hash, password_verify). Why not use MD5 or SHA1?Apr 17, 2025 am 12:06 AM

In PHP, password_hash and password_verify functions should be used to implement secure password hashing, and MD5 or SHA1 should not be used. 1) password_hash generates a hash containing salt values ​​to enhance security. 2) Password_verify verify password and ensure security by comparing hash values. 3) MD5 and SHA1 are vulnerable and lack salt values, and are not suitable for modern password security.

PHP: An Introduction to the Server-Side Scripting LanguagePHP: An Introduction to the Server-Side Scripting LanguageApr 16, 2025 am 12:18 AM

PHP is a server-side scripting language used for dynamic web development and server-side applications. 1.PHP is an interpreted language that does not require compilation and is suitable for rapid development. 2. PHP code is embedded in HTML, making it easy to develop web pages. 3. PHP processes server-side logic, generates HTML output, and supports user interaction and data processing. 4. PHP can interact with the database, process form submission, and execute server-side tasks.

PHP and the Web: Exploring its Long-Term ImpactPHP and the Web: Exploring its Long-Term ImpactApr 16, 2025 am 12:17 AM

PHP has shaped the network over the past few decades and will continue to play an important role in web development. 1) PHP originated in 1994 and has become the first choice for developers due to its ease of use and seamless integration with MySQL. 2) Its core functions include generating dynamic content and integrating with the database, allowing the website to be updated in real time and displayed in personalized manner. 3) The wide application and ecosystem of PHP have driven its long-term impact, but it also faces version updates and security challenges. 4) Performance improvements in recent years, such as the release of PHP7, enable it to compete with modern languages. 5) In the future, PHP needs to deal with new challenges such as containerization and microservices, but its flexibility and active community make it adaptable.

Why Use PHP? Advantages and Benefits ExplainedWhy Use PHP? Advantages and Benefits ExplainedApr 16, 2025 am 12:16 AM

The core benefits of PHP include ease of learning, strong web development support, rich libraries and frameworks, high performance and scalability, cross-platform compatibility, and cost-effectiveness. 1) Easy to learn and use, suitable for beginners; 2) Good integration with web servers and supports multiple databases; 3) Have powerful frameworks such as Laravel; 4) High performance can be achieved through optimization; 5) Support multiple operating systems; 6) Open source to reduce development costs.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools