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
https://forums.phpfreaks.com/topic/232255-session-problem/
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
https://forums.phpfreaks.com/topic/232255-session-problem/#findComment-1195112
Share on other sites

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.