2016-04-23 7 views
0

私は最近、自分のウェブサイト用のカスタム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

ありがとう!

+2

質問は、admin_check.phpのセッションを開始しましたか? –

+0

'session_start();'はセッションを正しく自動的に開始します。しかし、セッションからデータにアクセスしたり、セッションデータを操作したり、セッションにデータを格納したい場合は、そのページで 'session_start();'が強制されます。 –

+0

'$ _SESSION []'にアクセスする前に 'session_start()'を呼び出すだけです。 'session_register()'、 'session_is_registered()'、 'session_unregister()'については忘れてください。彼らはもはや存在せず、10年以上前から彼らは役に立たなかった。 – axiac

答えて

2

だからチュートリアルでは、すべてのページでセッションを開始する必要が言及しなかったが、それはありません。

したがってsession_start();ですが、セッションを使用しているすべてのファイルの一部である必要があります。

エラー報告は、ここであなたを助けているだろう:チュートリアルではカバーされていない可能性があることhttp://php.net/manual/en/function.error-reporting.php

もう一つは、「パスワード」です。

使用次のいずれか:

その他のリンク(PHP < 5.5の場合):

列の長さに関する重要な追記:

もし、あなた使用することを決めるpassword_hash()または互換パック(PHP < 5.5の場合)https://github.com/ircmaxell/password_compat/の場合、現在のパスワードの列の長さが60より小さい場合は、それ以上(またはそれ以上)に変更する必要があることに注意することが重要です。このマニュアルでは255の長さが推奨されています。

有効にするには、新しいハッシュを使用して列の長さを変更し、最初からやり直す必要があります。そうしないと、MySQLは自動的に失敗します。

後で上のデータベースを使用する予定の場合は、これは単なる洞察であるprepared statementsまたはmysqli_* with prepared statements

  • PDOを使用しています。
+0

はい、かなり基本的なチュートリアルです。それはsession_start()を持つと述べました。 admin_check.phpではなく、すべてのドキュメントに表示されます。私はツタからソースコードをダウンロードし、session_start()もありませんでした。 admin_check.phpにあります。だから私は分かりませんでした。私が言ったように、私はPHPで新しく、私は自分自身を教えています。助けてくれてありがとう!あなたはすべて素晴らしいです! –

+0

@JosephMontesあなたはジョセフを歓迎しています。*歓声* –

+0

downvoter、気をつけてください?それとも、それはおそらく...男。 * sigh * - pffft:p –

0

あなたのコードを見ると、PHPファイルの先頭にsession_start()を忘れてしまったので、これをPHPファイルの先頭に置くと良いでしょう。これは通常、ユーザーを識別して$_SESSION変数が機能するように設定します(または既に渡された)Cookieを設定します(または使用します)。

http://php.net/manual/en/function.session-start.php

+0

それは答えでした!私は手紙によるチュートリアルの手紙にちょうど従っていました。session_start()はありませんでした。 admin_check.phpには、session_start()は必要ないと思っていました。 admin_check.phpファイルにあります。今私は知っている!ありがとうございました –

0

問題:あなたのコード内でセッションを設定する前にセッションを開始するのを忘れました。

ソリューション: は、固定され、この1でのコードの最初の部分を置き換えます。

<?php 
session_start(); 
$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(); 
} 
} 
?> 

注:$_SESSION配列またはしようとしてにアクセスしようとする前に、は、ページ上のsession_start()ステートメントを実行することを忘れないでください1つを設定し、ブラウザに出力が送信される前に実行されます。

参考URL:

http://php.net/manual/en/session.examples.basic.php

+1

ありがとう!私はPHPの土地で愚かな人だと思う。私はチュートリアルの手紙を手紙にしていただけで、session_start()もありませんでした。チュートリアルのadmin_check.phpにあります。ありがとうございました! –

+0

@JosephMontes:いいえ問題。あなたはようこそ!...!あなたが必要であれば、もっと援助を求めることを躊躇しないでください! –

関連する問題