Jump to content

[SOLVED] Session problem making my brain hurt...


matthew798

Recommended Posts

OK. I have a problem i cannot fix. No matter what i try. I cannot get the session variable $_SESSION['username'] to carry from one page to another.

 

There are three pages (more in the whole site, but if i get it working on these three then i can do the rest)

-Login page

-Login process

-admin page

 

Here is the login page (pertinent part):

<form action="loginprocess.php" method="POST">
<span class="style1">Username</span><br />
<input name="username" class="textinputhead"></textarea><br />
<span class="style1">Password</span><br />
<input name="password" class="textinputhead"></textarea><br /><br />
<input type="submit" class="textinputsubmit" />
</form>

 

here is loginprocess.php:

<?php
session_start();
include 'dbconnect.php';
$username = mysql_real_escape_string($_POST['username']);
$password = mysql_real_escape_string($_POST['password']);

$sql="SELECT * FROM users WHERE username='$username' and password='$password'";
$result=mysql_query($sql);

$count=mysql_num_rows($result);

if($count==1){
session_register('$username'); 
header('location:test.php');
}
else {
echo "Wrong Username or Password. Use the back button. If you think this message is incorrect, contact the webmaster.";
}
?>

 

here is an admin page that would use info from the session created in loginprocess.php:

<?php
session_start();
$username = $_SESSION['username'];
include 'dbconnect.php';

$q = mysql_query("SELECT permission FROM users WHERE username = '$username'")
or die(mysql_error());

$row = mysql_fetch_assoc($q);
$permissions = explode(",", $row['permission']);

if ( $permissions['0'] == 0 ){
die('You do not have permission to access this administrative function');
}

?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<LINK REL=StyleSheet HREF="stylemain.css" TYPE="text/css" MEDIA=screen />
</head>

<body bgcolor="black">

<table border="0">
  <tr>
    <td><form action="newsprocess.php" method="POST">
      <span class='headline'>Title</span><br />
      <input name="title" type="text" class='textinputhead' />
<br /><br />
<span class='headline'>Body</span><br />
<textarea name="body" type="text" class='textinputbody' /></textarea><br /><br />
      <input type="submit" value='Post News' class='textinputsubmit'/>
    </form></td>

  </tr>
</table>

</body>
</html>

 

I have tried everything my poor little brain could think of. Different quotes, with and without the $, brackets, no brackets. But whatever i do the bloody session variable does not carry!

 

Important to note: The variable doesn't come up as undefined, when i echo $_SESSION['username'] on the admin page, it echoes blank but does not give me an error.

 

PLEASE. I am the type of person that won't be able to sleep tonight if i can't figure this out. I have to get things done. ANYTHING will help.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.