473,385 Members | 1,766 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Sessions not working in subsequent pages

Hi,

I am developing a website. In Login form , on successful login of user, i had created session variable $_SESSION['username'] and redirected to second page. In second page when i checked , isset($_SESION['username']) is returning false.

This is the first website I am developing. Do I have to change any settings in php.ini for sessions to work?
Nov 14 '08 #1
15 2350
Markus
6,050 Expert 4TB
Hi,

I am developing a website. In Login form , on successful login of user, i had created session variable $_SESSION['username'] and redirected to second page. In second page when i checked , isset($_SESION['username']) is returning false.

This is the first website I am developing. Do I have to change any settings in php.ini for sessions to work?
For us to *really* help and not just guess, we need to see your code. Please post it using [code] tags.

My guess is you're not starting the session with session_start().
Nov 14 '08 #2
Here is the code:

loginpage.php


Expand|Select|Wrap|Line Numbers
  1. <?php
  2. session_start();
  3. require('config.php');
  4.  
  5. if($_POST['submit'])
  6. {
  7.   $username = $_POST['name'];
  8.   $password = $_POST['password'];
  9.   $mysql = mysql_connect($dbhost,$dbuser,$dbpassword);
  10.   mysql_select_db($dbdatabase,$mysql);
  11.   $loginsql = "select * from logins where username='" . $username ."' and password='". $password. "';";
  12.   $loginres = mysql_query($loginsql);
  13.   $numlogin = mysql_num_rows($loginres);
  14.   $row = mysql_fetch_assoc($loginres);
  15.   if($numlogin==1)
  16.    {
  17.     session_register('USERNAME');
  18.     session_register('SESS_LOGGEDIN');
  19.     $_SESSION['USERNAME'] = $row['username'];
  20.     $_SESSION['SESS_LOGGEDIN'] = 1;
  21.     if(isset($_SESSION['USERNAME']))
  22.      {
  23.        header("Location:".$config_basedir."index.php");
  24.  
  25.      }
  26.       else echo"sessions not set";
  27.    }
  28.    else
  29.      echo "Incorrect Login, please try again";
  30.  
  31.  
  32.  
  33. }
  34. require('header.php');
  35.  
  36.  
  37.  
  38. ?>
  39.  
  40. <form name='loginForm' action='<?php $SCRIPT_NAME?>' method='POST'>
  41.  
  42. <table>
  43. <tr>
  44. <td> UserName : </td> <td><input type='text' name='name'></td>
  45. </tr>
  46. <tr>
  47. <td> Password : </td> <td><input type='password' name='password'></td>
  48. </tr>
  49. <tr>
  50. <td> </td> <td><input type='submit' name='submit' value='Login'></td>
  51. </td>
  52. </table>
  53.  
  54. </form>
  55.  
  56.  
  57.  
  58.  
  59. <?php
  60. require('footer.php');
  61. ?>
On successfull login of user , page is redirected to index.php which is using header.php

index.php

Expand|Select|Wrap|Line Numbers
  1. <?php
  2.  
  3.  require('header.php');
  4. ?>

header.php


Expand|Select|Wrap|Line Numbers
  1. <?php
  2. session_start();
  3.  require('config.php');
  4.  $db = mysql_connect($dbhost,$dbuser,$dbpassword);
  5.  mysql_select_db($dbdatabase,$db);
  6. ?>
  7.  
  8. <html>
  9.  
  10. <head>
  11. <title><?php echo $config_blogname; ?> </title>
  12. <link rel='stylesheet' href='stylesheet.css' tyep='text/css'>
  13. </head>
  14.  
  15. <body>
  16. <div id='header'>
  17. <h1><?php echo $config_blogname; ?></h1>
  18. </div>
  19. <div id='menu'>
  20. <a href='index.php'>Home</a>
  21. <a href = 'viewcat.php'> Categories </a>
  22. <?php
  23. if(isset($_SESSION['USERNAME']))
  24. {
  25.    echo "<a href='logout.php'> Logout</a>".$_SESSION['USERNAME'];
  26.    echo "<a href='addcat.php'> Add Category </a>";
  27.    echo "<a href='addentry.php'> Add Entry </a>";
  28. }
  29. else
  30.   echo "<a href='loginpage.php'> Login </a> <a href='signup.php'> SignUp </a> ";
  31.  
  32. ?>
  33. </div>
  34. <div id="bar">
  35. <img src= "../shoppingcart/Sai.jpg" width="200px" height="250px" top="200px">
  36. </div>
  37. <div id='main'>
In header.php isset($_SESSION['USERNAME']) is returning false.
Nov 15 '08 #3
maheswaran
190 100+
have u check in loginpage line 16 $row['username']; is return any thing?
Nov 15 '08 #4
Yes. It is returning username correctly. In loginpage
isset($_SESSION['USERNAME']) is returning true. But echo $_SESSION['USERNAME'] is not printing anything.
Nov 15 '08 #5
try changing your code in the index.php to:

Expand|Select|Wrap|Line Numbers
  1. <?php
  2.           session_start();
  3.           require('header.php');
  4. ?>
  5.  
  6.  
I hope this helps!
Nov 18 '08 #6
Markus
6,050 Expert 4TB
try changing your code in the index.php to:

Expand|Select|Wrap|Line Numbers
  1. <?php
  2.           session_start();
  3.           require('header.php');
  4. ?>
  5.  
  6.  
I hope this helps!
That shouldn't change anything but give you a notice for starting the session again.
Nov 18 '08 #7
Markus
6,050 Expert 4TB
Take lines 17 and 18 out of loginpage.php - this won't be the problem, but you don't need to use them.

I can't see anything wrong with what you posted.

We'll have to see if someone else can spot it.
Nov 18 '08 #8
try changing your code in the index.php to:

Expand|Select|Wrap|Line Numbers
  1. <?php
  2.           session_start();
  3.           require('header.php');
  4. ?>
  5.  
  6.  
I hope this helps!
I tried this. Its working. Thanks. It helped me continue two of my stucked projects. Thank you sooo much
Nov 19 '08 #9
Sessions are working fine in Internet Explorer. But not in Mozilla Firefox . Can anyone tell me why is that and what should I do for that to work?
Nov 19 '08 #10
Markus
6,050 Expert 4TB
I tried this. Its working. Thanks. It helped me continue two of my stucked projects. Thank you sooo much
I'll be damned..
Nov 19 '08 #11
I tried this. Its working. Thanks. It helped me continue two of my stucked projects. Thank you sooo much
glad i could be of help.. ^^,

and for your next problem, what happens in IE that does not happen in mozilla..? can you expound on the problem..?

I'll be damned..
ahahaha! XD lucky strike..?XD
Nov 19 '08 #12
glad i could be of help.. ^^,

and for your next problem, what happens in IE that does not happen in mozilla..? can you expound on the problem..?
The solution you have suggested (to add session_start() in index.php) is working in IE but not in mozilla. In mozilla firefox the problem still persists.
Nov 20 '08 #13
try to take out these lines from your login.php:

Expand|Select|Wrap|Line Numbers
  1. session_register('USERNAME');
  2. session_register('SESS_LOGGEDIN');
Nov 24 '08 #14
I tried but of no use.......
Nov 26 '08 #15
Markus
6,050 Expert 4TB
PHP is not browser-specific: it does not act differently in different browsers.

Try clearing your cache and cookies.
Nov 26 '08 #16

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: Chewy509 | last post by:
Hi Everyone, I'll just start, and say I am not a PHP developer (I'm a sysadmin, who has gotten lumped with a non-working website). But since I like to do this type of stuff, I though I might...
13
by: jing_li | last post by:
Hi, you all, I am a newbee for php and I need your help. One of my coworker and I are both developing a webpage for our project using php. We have a copy of the same files in different location...
1
by: Jon Grieve | last post by:
Hi, I've been playing for a few days now with putting Objects in sessions and maintaining that object during a session. This seems to be working just fine, and I can see and use most of the...
5
by: leegold2 | last post by:
Probably a newbie question about "state": My problem is I have a search form, so user enters a keyword <enter>, then this form posts to another page were the result are displayed. But this...
22
by: Theo | last post by:
Question for the group The authentication system for the site Im working on seems to function properly and all is good. A session keeps track of everything and a cookie is used to accept or deny...
3
by: LMachado1 | last post by:
I just started with php and I'm trying to make a simple interface as follows: - user is asked to input an integers, for example: how many students do you want to enter? - user is then shown a...
3
by: Gary W | last post by:
Hello, I have used SESSIONS on mission critical pages on my site, and if sessions are not enabled / supoorted - these pages will fail. When and why would a session fail? They do not store any...
7
by: Atte André Jensen | last post by:
Hi I'm developing a site where I'd like to store information during a users visit. So far I've been using sessions, but as far as I can tell it's not possible to control for how long a session...
13
Frinavale
by: Frinavale | last post by:
One of the most fundamental topics in web design is understanding how to pass information collected on one web page to another web page. There are many different ways you could do this: Cookies,...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.