sessions on localhost

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
sonic_2k_uk
Forum Newbie
Posts: 4
Joined: Tue Dec 02, 2003 10:13 am
Location: UK

sessions on localhost

Post by sonic_2k_uk »

hi.

ive had php installed and set up for some time with apache and mysql on my localhost. But ive come to using sessionsin php, and, well, they work fine on my server but not on my localhost!

Ive read through numerous posts on the internet with no luck. I dont suppose anyone else has a solution to this problem, or is having the same problem?

Ive tried settings the session.save_path = c:\php\sessiondata in the php.ini file, and i now know that the sessions are being written and created in this directory. But the problem comes when i try and use a login form which i know already works. This is the code im susing incase anyone wants to check it out:

index.php

Code: Select all

<?
 include "emailconfig.php";
 
 if(isset($username)) {
        mysql_connect($db_host, $db_username, $db_passwd);
	@mysql_select_db("$db_name") or die ("Unable to select database");

	$password = md5($password);
	$query = "Select * from $db_authtable where $db_usernamefield = '$username' and $db_passwordfield = '$password'";
	$res = mysql_query($query);
	if(@mysql_num_rows($res) < 1) {
		echo "<center><b><font size=2 color="#cc0000">Sorry, Invalid username/password!</font></b></center>";
	} else {
	    $row = mysql_fetch_array($res);
 	    $uid = $row[$db_userid];
	    session_register(uid);
		if ($uid == "admin") {
		header("Location: adminindex.php");
   	    exit();
		} else {
	    header("Location: mailbox.php");
   	    exit();
		}
        }
 }
?>

<html>
<link href="style.css" rel="stylesheet" type="text/css">
<body>

<div align="center">
  <p>E-mail Login</p>
  <form name="form1" method="post" action="index.php">
    <table width="100%"  border="1" bordercolor="<? echo "$bcolour" ?>">
      <tr>
        <td width="50%"><div align="right"><p><b>Username: </b></p></div></td>
        <td width="50%"><div align="left">
          <input name="username" type="text" class="formboxes" id="username">
        </div></td>
      </tr>
      <tr>
        <td><div align="right"><p><b>Password: </b></p></div></td>
        <td><div align="left">
          <input name="password" type="password" class="formboxes" id="password">
        </div></td>
      </tr>
      <tr>
        <td colspan="2"><div align="center">
          <input name="Enter" type="submit" class="formboxes" id="Enter" value="Enter">
        </div></td>
      </tr>
    </table>
  </form>
  </div>
</body>
</html>
adminindex.php

Code: Select all

<link href="style.css" rel="stylesheet" type="text/css">
<?
	include "emailconfig.php";
	 include "adminheader.php";

echo "<br><p><b>Welcome, Admin.</b></p><br><p>Please use the menu above to make any necessary changes.</p>";
?>
adminheader.php (same for members, minus the admin functions)

Code: Select all

<?
  include "emailconfig.php";
  global $uid, $db_host,$db_username,$db_passwd,$db_name;
  
  session_start();

  if(!isset($uid)) {
     header("Location: index.php");
     exit();
  }
 
  mysql_connect($db_host, $db_username, $db_passwd);
  @mysql_select_db("$db_name") or die ("Unable to select database");
  //error_reporting(E_ALL & ~E_NOTICE & ~E_WARNING);
?>

<html>
<title>Mail Box</title>
<link rel="stylesheet" TYPE="text/css" HREF="style.css">
<body>
<tr><td><tr><td><span class="box"><table width=100% border="1" align=center bordercolor="<? echo $bcolour ?>">
          <tr class="bg2">
              <td width=16% align=center><a href="adduser.php">Add User </a></td>
              <td width=16% align=center class=""><a href="edituser.php">Edit User </a></td>
              <td width=16% align=center class=""><a href="deleteuser.php">Delete User</a></td>
              <td width=16% align=center class="normal"><a href="account.php">Account</a></td>
              <td width=16% align=center class=""><a href="logout.php">Logout</a></td>
          </tr>
      </table>
</span>  
</body>
</html>
And after some testing the problem comes in the adminheader.php, because the session isnt working for some reason!

Code: Select all

<?

  if(!isset($uid)) {
     header("Location: index.php");
     exit();
  }

?>
If any can figure out anything id owe you one!

thanks

Andy Burton
?>
lachhekumar
Forum Newbie
Posts: 2
Joined: Tue Dec 02, 2003 10:17 am

Post by lachhekumar »

Hi

I also faced such type of problem i got the simple solution for that if it works for you then it is gr8

I think you are runing the PHP in the windows OS ( local ) and your server is in Linux ( Online )

When you register a session and use the header("location: index.php") this time the page get redirected but the PHP in windows is unable to write the session file so if you are using the windows system then redirect the page using the Javascript or meta tag
echo "<script language='javascript'>
location.href='<path>';
</script>
";

exit;
May this helped you

[ kumar ]
microthick
Forum Regular
Posts: 543
Joined: Wed Sep 24, 2003 2:15 pm
Location: Vancouver, BC

Post by microthick »

Code: Select all

session_register(uid);
You can only use session_register() if register_globals is on. Is it on?
sonic_2k_uk
Forum Newbie
Posts: 4
Joined: Tue Dec 02, 2003 10:13 am
Location: UK

Post by sonic_2k_uk »

hi, thanks for your reply

i tried changing index.php to:

Code: Select all

<?
 include "emailconfig.php";

$username = $_POST['username'];
$password = $_POST['password'];
 
 if(isset($username)) {
        mysql_connect($db_host, $db_username, $db_passwd);
	@mysql_select_db("$db_name") or die ("Unable to select database");

	$query = "Select * from $db_authtable where $db_usernamefield = '$username' and $db_passwordfield = '$password'";
	$res = mysql_query($query);
	if(@mysql_num_rows($res) < 1) {
		echo "<center><b><font size=2 color="#cc0000">Sorry, Invalid username/password!</font></b></center>";
	} else {
	    $row = mysql_fetch_array($res);
 	    $uid = $row[$db_userid];
	    session_register(uid);
		if ($uid == "admin") {
		echo "<script language='javascript'> 
location.href='adminindex.php'; 
</script>"; 
   	    exit();
		} else {
	    echo "<script language='javascript'> 
location.href='mailbox.php'; </script> 
"; 
   	    exit();
		}
        }
 }
?>
This makes the script think a bit longer but still doesnt work.

Yes im running windows xp, with apache, mysql and php. All latest version. My server is running php, mysql and unix. Not sure what versions.

my computer seems to write the session files because they contain data, but the other scripts doesnt seem to be picking it up!

Thanks
basdog22
Forum Contributor
Posts: 158
Joined: Sun Nov 30, 2003 3:03 pm
Location: Greece

Post by basdog22 »

Had the same problem. Fixed like this:

Open your php.ini
Make sure it is: session.save_handler = files
change the Save_SESSION_PATH
to
Save_SESSION_PATH= C:\windows\temp\
sonic_2k_uk
Forum Newbie
Posts: 4
Joined: Tue Dec 02, 2003 10:13 am
Location: UK

Post by sonic_2k_uk »

nope, doesnt do anything. im possitive its something to do with this code:

Code: Select all

<?

  include "emailconfig.php";
  global $uid, $db_host,$db_username,$db_passwd,$db_name;
  
  session_start();

  if(!isset($uid)) {
     header("Location: index.php");
     exit();
  }
 
  mysql_connect($db_host, $db_username, $db_passwd);
  @mysql_select_db("$db_name") or die ("Unable to select database");
?>
because ive changed the

Code: Select all

if(!isset($uid)) {
     header("Location: index.php");
     exit();
  }
to say

Code: Select all

if(!isset($uid)) {
     echo "no uid";
  }
and there is no uid. Any ideas?

Ive tried editing the php.ini file, and changing the "header" bit..

Andy Burton
basdog22
Forum Contributor
Posts: 158
Joined: Sun Nov 30, 2003 3:03 pm
Location: Greece

Post by basdog22 »

You must somehow make the $uid accessable from the script. What i did was to create a user block that holds the $username and include any file uppon user input like index.php?op=email . Index page holds the $email=email.php and include "$email.php"; I know it seems confusing but it works (any security tips?)
sonic_2k_uk
Forum Newbie
Posts: 4
Joined: Tue Dec 02, 2003 10:13 am
Location: UK

Post by sonic_2k_uk »

hi

thanks for your reply

i know i could do the index.php?uid=uid in a link but then yeh, security issues. Apart form that i know it would work, but, my script ive already got does work, just not on my server. And if i come to want to use sessions again i would like to get this sorted so i could use them!

Andy Burton
Post Reply