Jump to content

session problem


steve1388

Recommended Posts

This is my first post here. G'day everyone. I'm having trouble with getting a session recognized from one page to another. I did a search and used the advice but I'm still having trouble.

 

I have a page called checklogin.php that processes the information submitted for a member to login, and redirect it to login_success.php if the login was successful.

The problem I have is when I test the session in the second page it tells me there is no session. Here's my code (I omitted the db connection code):

 

<?php
// username and password sent from form 
$myusername=$_POST['user']; 
$mypassword=$_POST['pass'];
?>

<?
// 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:login_success.php");

}
else {
echo "Wrong Username or Password";
}
?>

 

after I am redirected to login_success.php:

 


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

if (isset($_SESSION['myusername']))
{
$loggedin = TRUE;
return $loggedin;
echo "logged in.";

}
else
echo "not logged in";
?>

 

It echoes "not logged in". I've struggled with this for days and I don't know what's wrong. Thanks in advance.

Link to comment
Share on other sites

Login.php

<?php
session_start();
if(isset($_POST['submit'])) {
  // check username and password from db or whatever
  if($sucessfulLogin) {
    $_SESSION['username'] = $username;
    header("Location: index.php");
  } else {
    echo "Incorrect login";
  }
}
?>

 

Index.php

<?php
if(!isset($_SESSION['username'])) {
    header("Location: login.php");
    exit();
} ?>
<html> <head>...
Welcome back, <?php echo $_SESSION['username']; ?>. Hope your having a good day!

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.