Jump to content

[SOLVED] Session help


garry

Recommended Posts

So I'm trying to make a login function for my website. I've managed to query the database and find out whether the information matches and whatnot. Now what I'm trying to do is make sessions. Here's the code I'm using:

 

<?php
if (isset($_POST['submitted'])) {

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

$query = "SELECT *
		  FROM users
		  WHERE username = '$username'
		  AND password = '$password'
		 ";

$result = mysql_query($query);

$row = mysql_fetch_array($result);

if (mysql_num_rows($result) == 0) {
	echo "Sorry, no match found!";
	die();
	}

if (mysql_num_rows($result) == 1) {

	session_start(); // Begin the session
	$_SESSION['username'] = $row['username'];

	echo "Welcome " . $row['firstname'] . "!";
	}

unset($_POST);
}

But I get a headers already sent error after starting the session :(

 

How can I tell it to start the session after the information has been verified?

 

And I was also wondering if someone could give me some help with explaining cookies and sessions and such. I know that sessions set a cookie on the users computer, but where do I put this information?

 

Any help is appreciated :)

Link to comment
Share on other sites

Put this line at the very top of the page and delete it from where it is now.

 

session_start(); // Begin the session

 

Sessions need to be started before anything is outputed.

 

The only line you need to change is the session_start() line

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.