Jump to content

[SOLVED] SESSION help! about to jump off a bridge


guyfromfl

Recommended Posts

I cannot figure out why this login script will not work.  I have simply copied and pasted it from code that works.  If I ask it to return $_SESSION['user_id'] from inside login.php it echos the correct value.  If you put it anywhere else it says undefiened index.

 

no matter what I get redirected to the login page.

 

I don't think the $_SESSION data is getting passed to the other pages.

 

PLEASE HELP

 

pretty simple code snipets:

 

header.php

<?php

include('includes/format.php');
include('includes/dblib.php');

$config = $db->getConfig();
$format = new format();

	session_start();

?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
	<title>Broadway Limited Imports Service Department</title>
	<link href="http://bli.servehttp.com/bli/knowledgebase/includes/bliprint.css" rel="stylesheet" type="text/css" media="print" />
	<link href="includes/css/bli.css" type="text/css" rel="stylesheet" media="screen, tv, tty" />
	<link href="http://bli.servehttp.com/bli/knowledgebase/includes/jquery-ui-1.7.2-custom.css" type="text/css" rel="stylesheet" />
	<script src='lib/jquery/jquery.js' type="text/javascript"></script>
	<script src='includes/corners.js' type="text/javascript"></script>
	<script src='includes/ui.core.js' type="text/javascript"></script>
	<script src='includes/ui.datepicker.js' type="text/javascript"></script>

<!-- Functions -->
	<script type="text/javascript">
$(function() {
$("#datepicker").datepicker();
$("#format").change(function() { $('#datepicker').datepicker('yy-mm-dd', {dateFormat: $(this).val()}); });

});
	</script>

</head>
<body>
<!-- <body onload="document.frmSearch.search.focus()"> -->
	<!-- Begin Wrapper -->
	<center>
		<div id="wrapper">
		<!-- Logged in Status -->
<?php

echo ($_SERVER['user_id']);
if (isset($_SESSION['user_id'])) {
	echo "<div style='float: right; font-size: 12px; padding-right: 10px; top:0' id='userPanel'>
		  <span style='color: #777;'><a href='issueList.php'>Issue Tracker</a> | </span>
		  <span style='color: #777;'><a href='admin/'>Admin Control Panel</a> | </span>   
	      <a href='logout.php'>Logged in as " . $db->getUserfNameById($_SESSION['user_id']) .
	     "</a></div>";
} else {
	echo "NOOOOOOOOOOOOOO";
}

?>
		<!-- Begin Header -->
		<div id="header">
			<p style="color:#ccc; valign: middle; text-align: left">
				<a href="<?php echo $config['companyWebsite']; ?>"><img style="padding-left: 10px" src="img/HeaderLogo.png" alt="Broadway Limited Imports" /></a>
				<a href="http://bli.servehttp.com">Knowledge Base</a>
			</p>
		</div>


		</div>
		<!-- End Header -->

 

 

index.php

<?php

require('includes/header.php');

//echo $_SESSION['user_id'];
//die();

if (isset($_SESSION['user_id'])) {
?>
		<h3>Main Menu</h3>
		<table id="menu">
			<tr>
				<td><img src='img/icons/128/Tools.png' /><br />Repairs</a></td>
				<td><img src='img/icons/128/Components.png' /><br />Parts</a></td>
			</tr>
			<tr>
				<td><img src='img/icons/128/Issue.png' /><br />Issues</td>
				<td><a href='customer.php?action=menu'><img src='img/icons/128/Customers.png' /><br />Customers</a></td>

			</tr>
		</table>

<?php
} else {
header("Location: login.php");
}


require('includes/footer.php'); 
?>


 

 

 

login.php

<?php

require('includes/header.php');
?>

<br /><br />
<div id="login">

<?php

// Check if user wants to login
if(isset($_GET['try'])) {

    // Check info was entered
    if(empty($_POST['username']) OR empty($_POST['password'])) {
    // not everything was entered
        echo "<span style='color: red'>Please fill in all the fields.</span>";
    } else {
        // Check credentials
        $username = mysql_real_escape_string($_POST['username']);
        //$password = md5($_POST['password']);
	$password = $_POST['password'];

        $checkUser = $db->checkUser($username, $password);

        if ($checkUser == 0) {
            echo "<span style='color: red'>Invalid login!</span>";
        } else {
            // CREATE SESSION
            $_SESSION['user_id'] = $checkUser;
		echo $_SESSION['user_id'];

            // $db->logUser();
            header("Location: index.php");
        }
    }
}

?>
<form action="login.php?try=validate" method="post">
<p>
<strong>Login<br /></strong>
Username: <input type="text" name="username" class="login" /><br />
<br />
Password: <input type="password" name="password" class="login" /><br />
<br />
<input type="submit" value="Login" />
</p>
</form>
</div><br /><br />
</div>
<?php
include ('includes/footer.php');
?>

Link to comment
Share on other sites

oh and the code for the $db->checkUser($username, $password); call

 

dblib.php

class dblib {
        // Other stuff

function checkUser($user, $pass) {
	/*
	 * Description: Validate a user by his log in creditials.  A
	 * 				user with user_id of zero is a normal web surfer
	 * 				and cannot access company privilege info.
	 * 
	 * Date:		April 1, 2009
	 */
        $sql = "SELECT id FROM bli.users WHERE
				bli.users.username='$user' AND
				bli.users.password=MD5('$pass')";
        $result = $this->query($sql);

        list($user_id) = mysql_fetch_row($result);

        if (empty($user_id)) {
            $user_id = 0;
        }

        return $user_id;
    }
   // More other stuff	
}

$db = new dblib();

Link to comment
Share on other sites

umm.. to tell you the truth I have no idea lol.

Best bet would be to google it.

I am not really a php/mysql expert, I am only 15 and started coding php like 2 years ago lol.

 

But if it doesn't madder what login script you use, I would definitely use that one I sent a few mins ago, I use that for all my projects, I find it pretty simple to use.

Don't know to much about servers and what not so I couldn't tell ya if that might be the problem but I haven't ever heard of a WAMP before only Linux.

Sorry

Link to comment
Share on other sites

I have two things ya might look into.

 

First, session variables for www.YourSite.com and YourSite.com (without the "www") are completely different.  I force my site to use the www with a if statement checking for it, else not header("Location: www.YourSite.com");

 

Another thing, you should use session_start(); first thing after your <?php on ALL pages including ones you include.  Even though they should inherit the parent document's session, it doesn't always work like that from what I saw in my own personal experience.  It was an easy fix though, I just started putting session_start(); at the top of all of my php pages.

 

Worth a shot if nothing else is working.

 

here's the snippet i use to force the "www", you can use .htaccess if you prefer.

 

<?php
if ( $_SERVER['SERVER_NAME'] != 'www.YourSite.com' )
{
	header("Location: http://www.YourSite.com/");
}
?>

Link to comment
Share on other sites

ugh i think i have it now.

 

for some retarded reason I put session_destroy in the the footer.php

 

I dont know whats worse, looking for a stupid problem for 9 hours and writing agro forum posts, or finding out that you did something stupid to make you stare at code for 9 hours and write agro forum posts.

Link to comment
Share on other sites

ugh i think i have it now.

 

for some retarded reason I put session_destroy in the the footer.php

 

I dont know whats worse, looking for a stupid problem for 9 hours and writing agro forum posts, or finding out that you did something stupid to make you stare at code for 9 hours and write agro forum posts.

oh well, it's all a part of coding :D glad you got the problem resolved

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.