0

hey people
i wrote this code and i got a problem , i cant register a session .
this is the login form :

<?php
if(!isset($_POST['username']))
{
$title = "- signin";
include "header.php";
include "nav.php";
?>
<form action="" method="post">
Username :<br /><input type="text" name="username" size="70" /><br />
Password :<br /><input type="password" name="password" size="70" /><br />
<input type="submit" value="login" /><br /><BR />
</form>
<?php
include "sb.php";
include "footer.php";
}
else
{
$username = $_POST['username'];
$password = $_POST['password'];
include "config.php"; // this is where my db informations and connections is 
$s = "SELECT*FROM members WHERE username = '$username' AND password = '$password'";
$q = mysql_query($s);
if(mysql_num_rows($q) == 0)
{
$title = "- error";
include "header.php";
include "nav.php";
echo "<h2>error</h2>";
echo "<p> we didnt found your infos in our databases , please login again from <a href='signin.php'>here </a> or <a href='signup.php'> sign up</a></p>";
include "sb.php";
include "footer.php";
}
else
{
while($row = mysql_fetch_array($q))
{
$name = $row['name'];
}
$_SESSION['username'] == $username;
$_SESSION['name'] == $name;
header("location:index.php");
echo "please wait ....";
}
mysql_close();
}
?>

so after filling this form . the form will send me to the index okay , i think up to here it is fine . so this is the index code :

<?php
$title = "- Home";
include "session.php"; // here is where the session is 
include "header.php";
include "nav.php";
include "main.php";
include "sb.php";
include "footer.php";
?>

here is the session.php file :

<?php
session_start();
if(isset($_SESSION['name']))
{
$name = $_SESSION['name']; 
}
else
{
$name == "gust";

}
?>

i wrote it like this couz i want to display the members name in my websites sidebar
so here is the code of the sidebar's code :

</div>
			</div>
			</div>
			<div><img src="images/spacer.gif" width="1" height="20px" alt="" /></div>
		<div><img src="images/spacer.gif" width="1" height="20px" alt="" /></div>
		<div><img src="images/spacer.gif" width="1" height="20px" alt="" /></div>
	  </div>
<div id="sidebar2" class="sidebar">
  <div class="r1">
  <h2>Actions</h2>
  <?php
  if(isset($name))
  {?>
  <ul>
 <li> welcome <?php echo $name; ?></li><br />
    <li><a href="add_blog_post.php">Add new blog post</a></li>
  <li><a href="add_new_article.php">Add new article</a></li>
   <?php
   }
   else
   {
   ?> <li><a href="signup.php">Signup</a></li>
  <li><a href="signin.php">Signin</a></li>
  <?php
  }
  ?>
  
 
 
 
 
  </ul>
  
  
    <h2>Categories</h2>
    <ul>
    <?php
	include "config.php";
	$s = "SELECT * FROM cats ";
	$q = mysql_query($s);
	while($row = mysql_fetch_array($q))
	{
	echo "<li><a href='".$row['cat_link']."' title = '".$row['cat_title']."'>".$row['cat_name']."(".$row['number_of_posts'].")</a></li>";
	}
      ?>
    </ul>
    <?php mysql_close(); ?>
  </div>
  <div><img src="images/spacer.gif" width="1" height="20px" alt="" /></div>
  <div class="r1">
    <h2>sugested links</h2>
    <ul>
    <?php
	include "config.php";
	$q = "SELECT * FROM links";
	$s = mysql_query($q);
	while($row = mysql_fetch_array($s))
	{
	echo " <li><a href='".$row['link']."' title='".$row['title']."' >".$row['name']."</a></li>";      }
	?>
    </ul>
    <?php
	mysql_close();
	?>
  </div>
  <div><img src="images/spacer.gif" width="1" height="20px" alt="" /></div>
  <div class="r1">
    <h2>advartisements </h2>
    <?php
	include "advars.php";
	?>
    
  </div>
</div>

now the problem is , it usually displays the guest part . it never displays the members part and also i want to user the session to allow the visitor to post an articles so i made another files called sessio_2.php it contains the code to allow the members to post an articles .
so here is the sessio_2.php

<?php
session_start();
if(isset($_SESSION['name']))
{
$name = $_SESSION['name'];
}
else
{
echo "you have to login ";
header("location:signin.php");
}
?>

but whenever i open the apage of adding new article it sends me to the login and then to the index .
please help me . i sepent the whole day trying to get the error and i didnt change anything
thanks

2
Contributors
3
Replies
4
Views
6 Years
Discussion Span
Last Post by Awah Mohamed
0

first thing that comes to mind is this:

$_SESSION['username'] == $username;
$_SESSION['name'] == $name;

should be:

$_SESSION['username'] = $username;
$_SESSION['name'] = $name;
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.