Jump to content

Recommended Posts

Hi all, searched the boards before posting but didn't find an answer. This is for a login script. The following code runs after user enters username/password and hits submit.

 

**This from auth.php

 

//pull record from DB based on username password _POST fields

$sql="SELECT table_id, table_fname FROM table WHERE table_email='$email' and table_password='$mypassword'";

$result=mysql_query($sql);

 

$count=mysql_num_rows($result);

if($count==1){

$table_id=mysql_result($result,0,"table_id");

$table_fname=mysql_result($result,0,"table_fname");

 

                //HERE is where I think my problem is

session_start();

$_SESSION['table_id'] = $table_id;

$_SESSION['table_fname'] = $table_fname;

$_SESSION['login']=1;

                //This line forwards correctly

header("location:page.php");

}

else {

//This works if bad username/password

echo "Wrong Username or Password";

//header("location:anotherpage.php");

}

 

The database is being called correctly and works. The header page redirects work correctly but my session variables don't seem to be gettig set. I'm developing this on a local box running the standard XAMP install. Is there a php.ini setting I need to address? Or am I calling the session_start() far too late in the coding?

 

I should note that $_SESSION['login']=1; does seem to work but I'm not sure it's getting set correctly (to 1 that is) or if it's just getting put into scope as empty. What I'm ultimately trying to do is use a header file include at the top of each of my site pages. If you have logged in, it displays a welcome message up top of screen, if not, you get the login form with username password fields.

 

**This from header.php which is an include in every page

 

if (!isset($_GET['login']) || $_GET['login'] = 0)

{

                show username/password form for haven't logged in yet....this works fine before logging in

}

else //LOGGED IN

{

This part woks sort of..seems to recognize a login  but if I try to reference my $_SESSION['table_fname'] in this block it breaks....

                In other words, this codeblock runs when it should, but _session variables give an error

}

 

Many thanks for any assistance on this. It feels like either my localhost server isn't processing session variables or I'm just trying to set them incorrectly.

Link to comment
https://forums.phpfreaks.com/topic/242040-session-variables-again/
Share on other sites

  • 2 weeks later...

Hi Mike. No the only place I ever call session_start() is in auth.php as the very first line of code, which is only ever run once when a user logs in. I moved the session_start() to the top of that code, the rest of the auth.php is the same as above. When I run this, all is well on my first login page, ie. the welcome message shows up in the header instead of the login form. However, when I click another link on the main menu of the site, I again get the login form even if the user has just logged in. So, it's as if the _session variables are getting set on the auth page, but going to another page kills the session or resets the variables. Here's the header.php code:

 

header.php

 

<div class="header-container">

<?php

if (!isset($_SESSION['login']))

{

print "<span class='header-login'>".$_SESSION['login'];

print "<input name='email' type='email' value='Email Address' length='20'>  ";

print "<input name='password' type='password' value='password' length='20'>";

print "<input name='submit' type='submit' value='Login'><br/>";

print "</span>";

print "<span class='header-login2'>";

print "<a href='register.php'>Register</a><font color='white'> | </font><a href='reset.php'>Forgot Password</a>";

print "</span>";

}

else

{

print "<span class='header-login'>".$_SESSION['login'];

print "<font color=white>Welcome ".$_SESSION['jcustomer_fname'].". Thanks for Visiting! </font>";

print "</span>";

}

?>

</div>

 

In this version, I'm checking to see if there's any value at all to _session['login'] and on the very first login this seems to work and is set to 1. Then I click a menu link on the site and it goes back to not existing, showing the login form instead.

 

Any thoughts?

Thanks!

Guest
This topic is now 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.