I am trying to set cookies in two different scenarios:
and then they will be directed to main.php where main.php includes header.php. Now all the problem is with header.php when users login they are redirected to main.php where header menu should be home, profile, logout etc but when users who are not logged in visit main.php. header menus should be sign up and login.
register.php
header.php
main.php is empty with just with hello world to test header working.
- When users sign up.
- When users login.
and then they will be directed to main.php where main.php includes header.php. Now all the problem is with header.php when users login they are redirected to main.php where header menu should be home, profile, logout etc but when users who are not logged in visit main.php. header menus should be sign up and login.
register.php
Code:
<?php
$con = mysqli_connect("localhost","root","","findfriends") or die ("Connection not established");
?>
<?php include ( "./inc/header.inc.php" ); ?>
<?php
if(isset($_POST['reg'])){
$fn=strip_tags(@$_POST['fname']);
$ln=strip_tags(@$_POST['lname']);
$un=strip_tags(@$_POST['username']);
$em=strip_tags(@$_POST['email']);
$pswd=strip_tags(@$_POST['password']);
$d= date("Y-m-d");
$reg_query = "INSERT INTO users (userid,username,first_name,last_name,email,password,sign_up_date,activated,bio,profile_photo,closed) VALUES ('','{$un}','{$fn}','{$ln}','{$em}','{$pswd}','{$d}','0','What you do?','','no')";
$reg_run = mysqli_query($con,$reg_query);
setcookie('user',$uname,time()+3600*24*365);
echo "The cookie has been set.";
header("Location:main.php");
}
?>
<?php
if(isset($_POST['login'])){
$log_que = "SELECT * FROM users WHERE username = '$un' AND password = '$pswd' ";
$log_run = mysqli_query($con, $log_que);
$row = mysql_fetch_array($log_run);
$un_db = $log_row['username'];
$pswd_db = $log_row['password'];
if($un == $un_db && $pswd == $pswd_db)
{
echo "LOGGED IN!";
setcookie('user',$uname,time()+3600*24*365);
header("Location:main.php");
}
}
?>
<div style="width: 800px; margin: 0px auto 0px auto;">
<table>
<tr>
<td width="60%" valign="top">
<h2>Already a member? Login Below</h2>
<form action="" method="POST">
<input type="text" name="user_login" size="25" placeholder="UserName"/><br><br>
<input type="password" name="password_login" size="30" placeholder="Passsword"/><br><br>
<input type="submit" name="login" value="Login">
</form>
</td>
<td width="40%" valign="top">
<h2>Sign Up!</h2>
<form action="" method="POST">
<input type="text" name="fname" size="25" placeholder="First Name"/><br><br>
<input type="text" name="lname" size="25" placeholder="Last Name"/><br><br>
<input type="text" name="username" size="25" placeholder="Username"/><br><br>
<input type="email" name="email" size="25" placeholder="Email"/><br><br>
<input type="password" name="password" size="25" placeholder="Password"/><br><br>
<input type="submit" name="reg" value="Sign Up!">
</form>
</td>
</tr>
</table>
Code:
<!DOCTYPE html>
<html>
<head>
<title>findfriends</title>
<script src="js/main.js" type="text/javascript"></script>
<link rel="stylesheet" type="text/css" href="./css/style.css"/>
</head>
<body>
<div class="headerMenu">
<div id="wrapper">
<div class="logo">
<img src="./img/logo.gif"/>
</div>
<div class="search_box">
<form action="searchresults.php" method="post" name="search">
<table>
<tr>
<td>
<input type="text" name="search" placeholder="Search ..."/>
</td>
<td>
<input type="image" src="./img/search-icon.png" alt="submit" />
</td>
</tr>
</table>
</form>
</div>
<div class="cb">
<a href="create_blog.php">Create a Blog</a>
</div>
<?php
if(isset($_COOKIE['user'])){
echo '
<ul class="dd">
<li><a href="main.php" >Home</a></li>
<li><a href="' . $user . '">Profile</a></li>
<li><a href="my_messages.php">Inbox' . $unread_numrows . '</a></li>
<li><a href="#">Management</a>
<ul><li><a href="account_settings.php">Settings</a></li>
<li><a href="logout.php">Logout</a></li>
</ul>
</li>
</ul>';
}
else
{
echo '
<ul class="dd">
<li><a href="register.php" >Sign Up</a></li>
<li><a href="register.php">Login</a></li>
</ul>';
}
?>
</div>
</div>
</body>
</html>