Postingan lainnya
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>
2 Jawaban:
<pre> // ini index nya <?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); }
?>
<form action="" method="get"> <input type="search" name="cari" placeholder="silahkan cari disini..."> </form>
<?php while($row = mysqli_fetch_assoc($articles)):?> <div class="each_article"> <h3><a href="view_article.php?id=<?php echo $row['id']; ?>"> <?php echo $row['judul']; ?></a></h3> <p> <?php echo excerpt($row['isi']); ?> </p> <p class="time"> <?php echo $row['waktu']; ?> </p> <p class="tag"> Tag: <?php echo $row['tag']; ?> </p>
&lt;?php if($login == true): ?&gt;
&lt;a href="edit.php?id=&lt;?php echo $row['id']; ?&gt;"&gt;Edit&lt;/a&gt;
&lt;a href="delete.php?id=&lt;?php echo $row['id']; ?&gt;"&gt;Delete&lt;/a&gt;
&lt;?php endif; ?&gt;
</div> <?php endwhile; ?>
<?php require_once "view/header.php"; require_once "view/footer.php"; ?> </pre>
<pre> // ini login nya <?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['user'] = $user;
header('Location: index.php');
}else{
$error = "Ada masalah saat login...!";
}
}else{ $error = "Username atau Password tidak boleh kosong...!"; }
}
require_once "view/header.php";
?>
<form action="" method="post"> <label for="username">username</label> <br> <input type="text" name="username" value=""> <br><br>
&lt;label for="password"&gt;password&lt;/label&gt; &lt;br&gt;
&lt;input type="password" name="password" value=""&gt; &lt;br&gt; &lt;br&gt;
&lt;input type="submit" name="submit" value="submit"&gt; &lt;br&gt;&lt;br&gt;
&lt;div id="error"&gt; &lt;?php echo $error; ?&gt; &lt;/div&gt;
</form>
<?php } ?> <?php require_once "view/footer.php"; ?> </pre>