Session Issue

Ok I have a logon page that I have written that uses adldap and I am tried to use the user name and pass it to the loggedin/logout page so I can input in the database but I’m having a little trouble please help.

This is the code for the Login page

<?php
session_start();

$uid = $_GET['userid']; 

require_once("adLDAP.php");

//create the LDAP connection
$adldap = new adLDAP();

if (isset($_POST['submit'])) { //Handle the form.

	//Register the user in the database.
//	require_once ('includes/mysql_connect.php');

//	function escape_data ($data){
//		global $dbc;
//		if(ini_get('magic_quotes_gpc')) {
//			$data = stripslashes($data);
//		}
//		return mysql_real_escape_string($data, $dbc);
//	}
	
	$message = NULL; //Create an empty new variable
	
	//check username
	if (empty($_POST['username'])) {
		$u = FALSE;
		$message .= '<p>You forgot to enter your username</p>';
	} else {
		$u =($_POST['username']);
	}
	
	//Check password
	if (empty($_POST['password'])) {
		$p = FALSE;
		$message .= '<p>You forgot to enter your password</p>';
	} else {
		$p = ($_POST['password']);
	}
	
	if ($u && $p){ //Everything is ok
	
if ($adldap -> authenticate($u,$p)){
	
session_name ('YourVisitID');
	
	
		//$_SESSION['username'] = $row[1];
		
	if($_SESSION['username']) {
	$id=$_SESSION['username'];
	//$_SESSION['username']=Null;
	header("Location: http://localhost/html/login.php"); 
	}else{
	header("Location: http://" . $_SERVER['HTTP_HOST'] . dirname($_SERVER['PHP_SELF']) . "/loggedin.php");	
	}
					exit(); // Quit the script.
	echo "<p>You are now logged in, {$_SESSION['username']}!</p>";
	
	
} else {

	echo ("Authentication failed!");
}	
			

	} else {
		$message .= '<p>Please try again. </p>';	

This is the code for the loggedin page.

<?php

session_name ('YourVisitID');
session_start(); // Start the session.

// If no session is present, redirect the user.
if (!isset($_SESSION['user_name'])) {
header("Location: http://" . $_SERVER['HTTP_HOST'] . dirname($_SERVER['PHP_SELF']) . "/login.php");	 	
}	
if(isset($_POST['submit'])) {
	require_once ('includes/mysql_connect.php');

	function escape_data ($data) {
		global $dbc;
		if (ini_get('magic_quotes_gpc')) {
			$data = stripslashes($data);
		}
		return mysql_real_escape_string($data, $dbc);
	}
	$message = NULL;
	if (empty($_POST['comment'])) {
		$c = FALSE;
		$message .= '<p>You forgot to enter your username!</p>';
	} else {
		$c = escape_data($_POST['comment']);
	}
	if ($c) {
	$query = "INSERT INTO comment (comment, userid, datetime) VALUES ('$c', '$u', NOW())";
	
	$result = @mysql_query ($query);
	if (mysql_affected_rows() >0) {
		header("Location: http://" . $_SERVER['HTTP_HOST'] . dirname($_SERVER['PHP_SELF']) . "/login.php");
	} else {
		$message .= '<p>Please try again.</p>';
	}
	}}
$u = $_SESSION['user_name'];
 echo "<p> Your name $u.</p>";

?>
<? echo "<p> Your name, {$_SESSION['username']}</p>";?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<p><label>Comment:</label> <input type="text" name="comment" size="120" maxlength="250" value="<?php if (isset($_POST['comment'])) echo $_POST['comment']; ?>" /></p>

<input type="submit" name="submit" value="Logout" />
<?
exit(); // Quit the script
ob_end_flush();

?>

Please help if you can.

And your question is???

What exactly is the problem you are having? Need a little more detail in the problem as well as things you have tried to resolve it, error messages, etc… so that we can try and figure it out.

it doesn’t give me the username in the loggedin page at all. IF i echo it out i get nothing

Have you tried Debugging? Using echo/print to trace the problem? Searched google and the PHP manual for any error messages you’re getting when using error_reporting()?

Sponsor our Newsletter | Privacy Policy | Terms of Service