Header dan session

jadi gini, saya buat sistem login, jika tidak ada session disana maka header akan menampilkan tombol login jika ada session maka header akan menampilkan tombol logout

tapi dari tadi belum bisa" kira kira dimana yah salah nya

<?php

// $login = true;
// if (isset($_SESSION['user'])){
// $login = false;
// }

?>

<!DOCTYPE html>
	<html>
	<head>
		<title>BLOG</title>
    <link rel="stylesheet" href="view/style.css">
	</head>
	<body>

    <header>
      <h1 id="header"> BLOG </h1>
    </header>

		<div id="menu">
			<a href="index.php">HOME</a>
			<a href="add.php">ADD</a>

			<?php if (!isset($_SESSION['user'])){ ?>
				<a href="login.php">LOGIN</a>
			<?php }else{ ?>
				<a href="logout.php">LOGOUT</a>
			<?php } ?>
		</div>

avatar HaradaKumiko
@HaradaKumiko

23 Kontribusi 6 Poin

Dipost 4 tahun yang lalu

2 Jawaban:

<pre> // ini index nya &lt;?php require_once "core/init.php";

$login = false; if (isset($_SESSION['user'])){ $login = true; }

$articles = show();

if( isset($_GET['cari']) ){ $cari = $_GET['cari']; $articles = hasil_cari($cari); }

?&gt;

&lt;form action="" method="get"&gt; &lt;input type="search" name="cari" placeholder="silahkan cari disini..."&gt; &lt;/form&gt;

&lt;?php while($row = mysqli_fetch_assoc($articles)):?&gt; &lt;div class="each_article"&gt; &lt;h3&gt;&lt;a href="view_article.php?id=&lt;?php echo $row['id']; ?&gt;"&gt; &lt;?php echo $row['judul']; ?&gt;&lt;/a&gt;&lt;/h3&gt; &lt;p&gt; &lt;?php echo excerpt($row['isi']); ?&gt; &lt;/p&gt; &lt;p class="time"&gt; &lt;?php echo $row['waktu']; ?&gt; &lt;/p&gt; &lt;p class="tag"&gt; Tag: &lt;?php echo $row['tag']; ?&gt; &lt;/p&gt;

  &amp;lt;?php if($login == true): ?&amp;gt;
  &amp;lt;a href=&quot;edit.php?id=&amp;lt;?php echo $row[&#039;id&#039;]; ?&amp;gt;&quot;&amp;gt;Edit&amp;lt;/a&amp;gt;
  &amp;lt;a href=&quot;delete.php?id=&amp;lt;?php echo $row[&#039;id&#039;]; ?&amp;gt;&quot;&amp;gt;Delete&amp;lt;/a&amp;gt;
  &amp;lt;?php endif; ?&amp;gt;

&lt;/div&gt; &lt;?php endwhile; ?&gt;

&lt;?php require_once "view/header.php"; require_once "view/footer.php"; ?&gt; </pre>

avatar HaradaKumiko
@HaradaKumiko

23 Kontribusi 6 Poin

Dipost 4 tahun yang lalu

<pre> // ini login nya &lt;?php

require_once "core/init.php";

if (isset($_SESSION['user'])){ header('Location: index.php'); }else{

$error = "";

if(isset($_POST['submit'])){ $user = $_POST['username']; $pass = $_POST['password'];

if( !empty(trim($user) ) && !empty(trim($pass) ) ){

if(cek_data_login($user, $pass)){
  $_SESSION[&#039;user&#039;] = $user;
  header(&#039;Location: index.php&#039;);
}else{
  $error = &quot;Ada masalah saat login...!&quot;;
}

}else{ $error = "Username atau Password tidak boleh kosong...!"; }

}

require_once "view/header.php";

?&gt;

&lt;form action="" method="post"&gt; &lt;label for="username"&gt;username&lt;/label&gt; &lt;br&gt; &lt;input type="text" name="username" value=""&gt; &lt;br&gt;&lt;br&gt;

  &amp;lt;label for=&quot;password&quot;&amp;gt;password&amp;lt;/label&amp;gt; &amp;lt;br&amp;gt;
    &amp;lt;input type=&quot;password&quot; name=&quot;password&quot; value=&quot;&quot;&amp;gt; &amp;lt;br&amp;gt; &amp;lt;br&amp;gt;

    &amp;lt;input type=&quot;submit&quot; name=&quot;submit&quot; value=&quot;submit&quot;&amp;gt; &amp;lt;br&amp;gt;&amp;lt;br&amp;gt;

    &amp;lt;div id=&quot;error&quot;&amp;gt; &amp;lt;?php echo $error; ?&amp;gt; &amp;lt;/div&amp;gt;

&lt;/form&gt;

&lt;?php } ?&gt; &lt;?php require_once "view/footer.php"; ?&gt; </pre>

avatar HaradaKumiko
@HaradaKumiko

23 Kontribusi 6 Poin

Dipost 4 tahun yang lalu

Login untuk ikut Jawaban