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
https://forums.phpfreaks.com/topic/105920-solved-session-help/
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.