私は最近、自分のウェブサイト用のカスタムCMSを作成する方法に関するチュートリアルに従っていました。私は現在、バックエンドを作っているので、ユーザーはページを作成し編集することができます。このチュートリアルは古いものなので、さまざまな機能が廃止されました。私は、 "session_register();"を除いて、ほとんどを修正することができました。関数。私は "$ _SESSION ['admin'] = $ username;を使用するように指示したものを含め、多くのサイトで見ました。例えば。これは多くのページに適用されるので、私のためには機能しません。各ページは情報を再入力するように求めています。PHPでセッションを正しく開始するには?
は、ここで私が使用していますファイルです:
admin_check.php:
<?php
$error_msg="";
if ($_POST['username']){
$username=$_POST['username'];
$password=$_POST['password'];
// Simple hard coded values for the correct username and password
$admin="Admin";
$adminpass="Password";
//connect to mysql here if you store admin username and password in your database
//This would be the prefered method of storing the values instead of hard coding them here into the script
if(($username !=$admin)||($password != $adminpass)){
$error_msg='<h3 class="text-danger">Login information incorrect, please try again.</h3>';
} else{
$_SESSION['admin']=$username;
require_once "index.php";
exit();
}
}
?>
<?php
if ($_SESSION['admin']!="Admin"){
echo '<div class="container" style="background-color: #FFF ;"><h3><i class="glyphicon glyphicon-alert text-danger"></i> Solo los administradores
tienen permiso para ver este directorio. No se admite gente con Lag! </h3><br/>
<!DOCTYPE html
<html>
<head>
<title>Login Administrador</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta charset="UTF-8">
<link href="../css/bootstrap.min.css" rel="stylesheet" />
<link href="../css/styles.css" type="text/css" rel="stylesheet">
<link href="//netdna.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap- glyphicons.css" rel="stylesheet">
</head>
<body style="padding-top: 5px; background-color: #EEE ;">
<div style="max-width: 450px; max-height: 550px; background-color: #CCC; padding-top: 30px; box-shadow: 0 0 15px #DDD; border-radius: 10px" class="container">
<div class="jumbotron" style="background-color: #fff; max-height: 470px; padding: 10px; border-radius: 2; box-shadow: 0 0 12px #777;">
<legend class="text-success">LOGIN ADMINISTRATOR</legend>
<hr />
<p style="font-size: 12pt;">Please enter your username and password to login into the admin site.</p>
<form action="admin_check.php" method="post" target="_self" class="form-group">
<label class="label label-default">username</label>
<input type="text" class="form-control" placeholder="username" name="username" id="username">
<br/>
<label class="label label-default">password</label>
<input type="password" class="form-control" placeholder="password" name="password" id="password">
<br/>
<button class="btn-success" value="submit" type="submit" name="button" id="button" >Submit</button>
</form><br/>
'.$error_msg.'
</div>
</div>
</body>
</html>
<br/><br/>
<center> <a href="../" >Click aquí para regresar a un lugar seguro.</a> </div></center>';
exit();
}
?>
^あなたが見ることができるように、これはここで管理者の入力私がしたいのですが、ユーザー名とパスワード、 Cookieのように何らかの形で保存されるため、ユーザ名とパスワードを何度も何度も入力しなくても、自由にナビゲートできます。
のindex.php:
<?php
session_start();
include_once "admin_check.php";?>
<?php
include "admin_header.php";
?>
<body id="home">
<div class="container" style="background-color: white;">
<h2 style="color: #FF691F"><i class="fa fa-home fa-2x"></i> Home</h2>
</div>
</body>
<?php include "admin_footer.php"; ?>
create_blog.php:このように
<?php
session_start();
include_once "admin_check.php";?>
<?php
include "admin_header.php";
?>
<script type="text/javascript">
function validate_form(){
valid=true;
if(document.form.pagetitle.value===""){
alert("Please Enter a Page Title!");
valid=false;
}else if{
alert("Please Enter Keywords about this Page!");
valid=false;
}
return valid;
}
</script>
<script type="text/javascript">
function validate_form1(){
valid=true;
if(document.form.pid.value===""){
alert("Please Enter a Page ID!");
valid=false;
}
return valid;
}
</script>
<body id="blog">
<div class="container" style="background-color: white; box-shadow: 0 -5px 9px 3px #b2b2b2;">
<br/>
<h2 style="color: #777 ;"><i class="fa fa-book fa-lg"></i> Upload Blog Entry</h2>
<p class="text-danger text-center">Title, Body, and Keywords are required to create new post! None should be left blank!</p>
<a href="mmoAdmin/"><h6 class="text-muted pull-right"><< Back Home</h6> </a>
<br/>
<form id="form1" name="form1" method="post" action="edit_blog.php" onsubmit="return validate_form1();" class="form-inline">
<div class="form-group">
<button class="btn-default" type="submit" name="button2" id="button2" value="Edit Post">Edit Post</button>
</div>
<input name="pid" type="text" id="pid" size="8" maxlength="11" class="form-control"/>
<span id="helpBlock1" class="help-block">Enter the ID of the post you want to edit. eg: "blog/article.php?pid=<'this is the id'>"</span>
</form>
<br/>
<form id="form2" name="form2" method="post" action="delete_blog.php" onsubmit="return validate_form2();" class="form-inline">
<div class="form-group">
<button class="btn-default" type="submit" id="button3" name="button3" value="Delete Post">Delete Post</button>
</div>
<div class="form-group"><input type="text" class="form-control" id="pid" name="pid"/></div>
<span id="helpBlock2" class="help-block">Enter the ID of the post you want to delete. eg: "blog/article.php?pid=<'this is the id'>"</span>
</form>
<br/>
<form id="form" name="form" method="post" action="congrat_blog.php" onsubmit="return validate_form();">
<div class="form-group">
<label>Title</label>
<input type="text" name="pagetitle" id="pagetitle" class="form-control" placeholder="Title..." value="<?php echo $pagetitle; ?>"/>
<br/>
<br/>
<label>Body</label>
<textarea name="pagebody" id="pagebody" style="height: 480px; width: 100%" ><?php echo $pagebody; ?></textarea>
<br/>
<label>Keywords</label>
<input type="text" name="keywords" id="keywords" class="form-control" placeholder="blog, mmob, etc..." value="<?php echo $keywords; ?>" />
<br/>
<button type="submit" class="btn-success" name="button" id="button" value="Create this page now">Submit</button>
</div>
</form>
</div>
</body>
<?php include "admin_footer.php"; ?>
^私は複数のページを持っている、PHPの機能を持つすべて "のsession_start();" "admin_check.php"を "$ _SESSION ['admin'] = $ username"という関数でinclude_onceしました。
注:admin_header.phpとadmin_footer.phpは、HTMLヘッダーのナビゲーションバーとフッターに過ぎませんので、個々のページに合わせて修正する必要はありません。
私はかなり新しいPHPです。私はこれを行うの正しい道に迷っていますか? 基本的には、管理者がバックエンドにアクセスして情報をデータベースにアップロードできるように、ログイン機能があります。そのため、サイトにデータが入力されます。
はここでチュートリアルシリーズのリンクは、私は、この基本的なCMSを作成するには、次のしてきたです。お時間を
How to Build PHP and MySQL CMS Website Software
ありがとう!
質問は、admin_check.phpのセッションを開始しましたか? –
'session_start();'はセッションを正しく自動的に開始します。しかし、セッションからデータにアクセスしたり、セッションデータを操作したり、セッションにデータを格納したい場合は、そのページで 'session_start();'が強制されます。 –
'$ _SESSION []'にアクセスする前に 'session_start()'を呼び出すだけです。 'session_register()'、 'session_is_registered()'、 'session_unregister()'については忘れてください。彼らはもはや存在せず、10年以上前から彼らは役に立たなかった。 – axiac