function ShortUrlCode($existed = array(), $num = 6) { $str = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; $len = strlen($str); $code = ''; for ($i = 0; $i < $num; $i++) { $k = rand(0, $len - 1); $code .= $str[$k]; } if (in_array($code, $existed)) { $code = ShortUrlCode($existed, $num); } return $code; }
$smarty->assign('modules', $modules); $smarty->display('project_create.html'); break; case 'create_submit': if (!$user->CheckToken(Val('token', 'POST'))) { ShowError('操作失败'); } $title = Val('title', 'POST'); $description = Val('description', 'POST'); if (empty($title)) { ShowError('项目名称不能为空', URL_ROOT . '/index.php?do=project&act=create'); } $db = DBConnect(); //生成短网址字符 $existedStrs = $db->FirstColumn("SELECT urlKey FROM " . Tb('project') . ""); $urlKey = ShortUrlCode($existedStrs); //生成authCode $authCode = md5('xsser_' . $urlKey . '_' . $user->userId . '_' . time()); $values = array('title' => $title, 'description' => $description, 'userId' => $user->userId, 'urlKey' => $urlKey, 'authCode' => $authCode, 'addTime' => time()); $db->AutoExecute(Tb('project'), $values); $projectId = $db->LastId(); //ShowSuccess('创建成功'); header("Location: " . URL_ROOT . '/index.php?do=project&act=setcode&ty=create&id=' . $projectId); break; case 'setcode': $db = DBConnect(); $id = Val('id', 'GET', 1); $ty = Val('ty', 'GET'); $project = $db->FirstRow("SELECT * FROM " . Tb('project') . " WHERE id='{$id}' AND userId='" . $user->userId . "'"); if (empty($project)) { ShowError('项目不存在或没有权限');