Jump to content

Archived

This topic is now archived and is closed to further replies.

spires

Session help please. early post Still unsolved!

Recommended Posts

Hi,

I've built a login system that once the user has entered in his usname and pword it will take
them to the next page.

This was all working earlyer today, but has since stop. Which means there must be something
wrong with my code.

Could someone please have a quick look?

[code]
<?php
session_start();
if(session_is_registered(username)) {
header('Location: add_banner.php');
}
include('dbconnect.php');
?>
<?php

$bodyErrors = array();

if (!empty($_POST['submit_butt'])) {
if ($_POST['username']=='')
$bodyErrors['username'] = 'Add Your Username';
if ($_POST['password']=='')
$bodyErrors['password'] = 'Add Your Password';

if (count($bodyErrors) == 0) {

$username=$_POST['username'];
$password=$_POST['password'];

$sql="SELECT * FROM user_info WHERE username='$username' and password='$password'";
$result = mysql_query($sql);
$count = mysql_num_rows($result);



if ($count == 1) {
session_register('username');
session_register('password');
header('Location: add_banner.php');

} else {
$noinput = '<div class="white2">Sorry. You have entered an incorrect username or password,<br> please try again';
}

  } else {
if (empty($username) || empty($password)) { 
$bodylogErrors = '<div class="error">';
foreach ($bodyErrors as $log_error) {
$bodylogErrors .= "<li>$log_error</li>";
}
$bodylogErrors .= '</div>';
   }
}

}

include('functions/wrapper1.php');
include('functions/dropdowns.php');
include('functions/random_images.php');

?>
[/code]

Now, here is the strange part.
The page is being directed to add_banner.php but then comes back because i have a:
[code]
session_start();
if(!session_is_registered(username)) {
header('Location: add_a_banner.php');
}
[/code]
This means that the sessions are not registering.

Also, if i echo out the username inbetween session_register and the header
e.g
[code]
session_register('username');
session_register('password');
                         echo $username;
header('Location: add_banner.php');
[/code]
The session DO register, but then the header dont work because something is being printed to screen.

Has anyone seen this before?
any help please.

:D

Share this post


Link to post
Share on other sites
Use this:
[code]$_SESSION['username'] = $username;
$_SESSION['password'] = $password;[/code]

Instead of
[code]session_register('username');
session_register('password');[/code]
Also all what session_register does is setup a blank username/password session variable. It doesnt pupulate the session with anything. Also session_register is depreciated. You should use the code provided above to create your session variables.

In add_banner.php you should use this:
[code=php:0]session_start();
if(!isset($_SESSION['username'])) {
header('Location: add_a_banner.php');
}[/code]

Share this post


Link to post
Share on other sites
I Cant for the life of me see whats wrong.

Has anyone every come across this problem when the sessions will not register.
It seems as though the header is kicking in before the session registers?

Any more suggestion please.

Share this post


Link to post
Share on other sites
Thanks for looking
Here you go

login page
[code]
<?php
session_start();
if(isset($_SESSION['username'])) {
header('Location: add_banner.php');
}

include('validation.php');
include('dbconnect.php');
?>
<?php
$bodyErrors = array();

if (!empty($_POST['submit_butt'])) {
if ($_POST['username']=='')
$bodyErrors['username'] = 'Add Your Username';
if ($_POST['password']=='')
$bodyErrors['password'] = 'Add Your Password';

if (count($bodyErrors) == 0) {

$username=$_POST['username'];
$password=$_POST['password'];

$sql="SELECT * FROM user_info WHERE username='$username' and password='$password'";
$result = mysql_query($sql);
$count = mysql_num_rows($result);



if ($count == 1) {
session_register('username');
session_register('password');
header('Location: add_banner.php');

} else {
$noinput = '<div class="white2">Sorry. You have entered an incorrect username or password,<br> please try again';
}

  } else {
if (empty($username) || empty($password)) { 
$bodylogErrors = '<div class="error">';
foreach ($bodyErrors as $log_error) {
$bodylogErrors .= "<li>$log_error</li>";
}
$bodylogErrors .= '</div>';
   }
}

}

include('functions/wrapper1.php');
include('functions/dropdowns.php');
include('functions/random_images.php');

?>
[/code]


second page
[code]
<?php
session_start();
if(!isset($_SESSION['username'])) {
header('Location: add_a_banner.php');
}


include('validation.php');
include('dbconnect.php');
?>
[/code]

Share this post


Link to post
Share on other sites
i think if you look into the other included files.

your see that they have no session_start() in so, I think you should add them and try also i strongly recomend you change all the session register to the way as posted  above ok.

Share this post


Link to post
Share on other sites
Sorry, i thought i had changed it all. silly me.  :P

So, you have to have a session_start on every page related?
That could very well be the problem. I shall go try it out and let you know how i get on.

Cheers mate.

Share this post


Link to post
Share on other sites
Ok, I've added session_start() to every page that is included.
and changed all of the sessions as recommended above.
But i still not registering, i can see the address bar change which means the header is working.

here is the web address
http://www.theeventorganiser.com/add_a_banner.php

here the code for the page the whole php
[code]
<?php
session_start();
if(isset($_SESSION['username'])) {
header('Location: add_banner.php');
}

include('validation.php');
include('dbconnect.php');
?>
<?php
$submit_cat = $_POST['cat'];
$loginErrors = array();

if (!empty($_POST['Submit'])) {
if ($_POST['username']=='')
$loginErrors['username'] = 'Add Your Username';
if ($_POST['password']=='')
$loginErrors['password'] = 'Add Your Password';

if (count($loginErrors) == 0) {

$username=$_POST['username'];
$password=$_POST['password'];


$sql="SELECT * FROM user_info WHERE username='$username' and password='$password'";
$result = mysql_query($sql);
$count = mysql_num_rows($result);



if ($count == 1) {
$_SESSION['username'] = $username;
$_SESSION['password'] = $password;
header("Location:profile.php");

} else {
$noinput = '<div class="white2">Sorry. You have entered an incorrect username or password,<br> please try again';
}

  } else {
if (empty($username) || empty($password)) { 
$strLogError = '<div class="white2">';
foreach ($loginErrors as $log_error) {
$strLogError .= "<li>$log_error</li>";
}
$strLogError .= '</div>';
  }
}

}


$bodyErrors = array();

if (!empty($_POST['submit_butt'])) {
if ($_POST['username']=='')
$bodyErrors['username'] = 'Add Your Username';
if ($_POST['password']=='')
$bodyErrors['password'] = 'Add Your Password';

if (count($bodyErrors) == 0) {

$username=$_POST['username'];
$password=$_POST['password'];

$sql="SELECT * FROM user_info WHERE username='$username' and password='$password'";
$result = mysql_query($sql);
$count = mysql_num_rows($result);



if ($count == 1) {
$_SESSION['username'] = $username;
$_SESSION['password'] = $password;
header('Location: add_banner.php');

} else {
$noinput = '<div class="white2">Sorry. You have entered an incorrect username or password,<br> please try again';
}

  } else {
if (empty($username) || empty($password)) { 
$bodylogErrors = '<div class="error">';
foreach ($bodyErrors as $log_error) {
$bodylogErrors .= "<li>$log_error</li>";
}
$bodylogErrors .= '</div>';
  }
}

}

include('functions/wrapper1.php');
include('functions/dropdowns.php');
include('functions/random_images.php');

?>
[/code]


This is the code on the second page
[code]
<?php
session_start();
if(!isset($_SESSION['username'])) {
header('Location: add_a_banner.php');
}


include('validation.php');
include('dbconnect.php');
?>
[/code]

Any more help would be greatly appreceated.

Thanks for taking the time to do this.

Share this post


Link to post
Share on other sites
very strange all worked then went dead aswell weired.

echo out your querys and see if they mach as it suppose to not there is also some pages got no links.

when i press your account i get a hello so i think the sessions work.

it's the querys i think have a look weired

Nice site theo.

thinking.

Share this post


Link to post
Share on other sites
If i echo the $username out after the:   $_SESSION['username'] = $username;
                         $_SESSION['password'] = $password;
It echos out the user name, Its also registers the session, Which is very strange?
But then the header dont work.

e,g
$_SESSION['username'] = $username;
$_SESSION['password'] = $password;
echo $username;
header('Location: add_banner.php');

the 'Your Accout' doesn't have any sessions on the page its goes to yet.

Here is a test username and password
test
test

Share this post


Link to post
Share on other sites
i logged in and i got the hello very weired.

mybe you need to use include dont knoe weired


this is for the users to login as the login page i suspect yes?

include("add_banner.php");

Share this post


Link to post
Share on other sites
[quote author=spires link=topic=106605.msg426703#msg426703 date=1157223368]
If i echo the $username out after the:  $_SESSION['username'] = $username;
                          $_SESSION['password'] = $password;
It echos out the user name, Its also registers the session, Which is very strange?
But then the header dont work.

e,g
$_SESSION['username'] = $username;
$_SESSION['password'] = $password;
echo $username;
header('Location: add_banner.php');

the 'Your Accout' doesn't have any sessions on the page its goes to yet.

Here is a test username and password
test
test
[/quote]
$username is not your session variable. That variable is the variable you setup ealier to to get the username form the _POST['username'] array. To echo the username session you'll want to use [code=php:0]echo $_SESSION['username'];[/code]

Share this post


Link to post
Share on other sites
the login box to the bottom left donn't have any session check on the next page yet.

The login box in the center does, so try in that box and you will see the address baar change, then change back.

Share this post


Link to post
Share on other sites
the login box to the bottom left donn't have any session check on the next page yet.

The login box in the center does, so try in that box and you will see the address baar change, then change back.

Ok wildteen88, I'll try it out.

Share this post


Link to post
Share on other sites
Yeah, it echos out the user name.

If you try the test=username and test=password your'll see it echo out at the top left hand corner

Share this post


Link to post
Share on other sites

×

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.