Jump to content

Session not working


justinede

Recommended Posts

Hey Guys,

 

I have a really simply login script, but it isnt working.

For some reason, the variable myusername is not being stored in the session correctly.

 

Here is my checklogin.php

 

<?php
ob_start();
session_start();
$host="****"; // Host name
$username="*****"; // Mysql username
$password="******"; // Mysql password
$db_name="******t"; // Database name
$tbl_name="members"; // Table name

// Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");

// Define $myusername and $mypassword
$myusername=$_POST['myusername'];
$mypassword=$_POST['mypassword'];

// To protect MySQL injection (more detail about MySQL injection)
$myusername = stripslashes($myusername);
$mypassword = stripslashes($mypassword);
$myusername = mysql_real_escape_string($myusername);
$mypassword = mysql_real_escape_string($mypassword);

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

// Mysql_num_row is counting table row
$count=mysql_num_rows($result);
// If result matched $myusername and $mypassword, table row must be 1 row

if($count==1){
// Register $myusername, $mypassword and redirect to file "login_success.php"
$_SESSION['myusername'] = $myusername;
$_SESSION['mypassword'] = $mypassword;
header("location:users/$myusername/");
}
else {
echo "Wrong Username or Password";
}

ob_end_flush();
?>

 

the script directs you to the right location, but when we get there, the session has lost the myusername variable. here is the user's page.

 

<?php
session_start();
echo $_SESSION['myusername'];
?>

 

All I get is a blanc page. :/

 

Thanks for any help.

Link to comment
Share on other sites

What are the complete URLs (with the http://www.domain.com/path/page_name.php, xxxxxx out the domain if you don't want to post it) of both the checklogin.php page and the users/username/ page?

 

Also, what does a phpinfo() statement show for the session.cookie_path and session.cookie_domain settings?

Link to comment
Share on other sites

Assuming that you didn't xxxxx out a www. vs no-www. on either of the URLs, add the following two lines of code at the line after the line with your first opening <?php tag in both files -

 

ini_set("display_errors", "1");
error_reporting(E_ALL);

Link to comment
Share on other sites

If all the information you have posted is accurate, best guesses are that either you have disabled cookies in your browser or session handling is disabled or is not working as expected on your server.

 

After you attempt this and your browser is still open, look at the cookies or the page info in your browser and check if you are getting a cookie named PHPSESSID under your web site URL.

Link to comment
Share on other sites

Your use of ob_start(); and ob_end_flush(); could be hiding any errors being reported/displayed in the first code.

 

Just remove those two statements (you should only use output buffering if you want to capture output, not to fix problems in your logic.)

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.