Having a hard time creating sessions In PHP?
Posted: Sat May 12, 2012 1:59 pm
Hi,
I'm new to this forum. They is my first post and I'm currently learning how to code via youtube videos!
I'm trying to create a session_start(); for my website. Every time I login I'm getting a nasty error message and not sure why.
Would you guys happen to know what I'm doing wrong? I don't think I'm mistyping session_start(); incorrectly. Do I need to declare something in the session? I'm confused ......
Thanks everyone in advance!
http://whatsmyowncarworth.com/login/login.php
User info
username = alex
password = abc
username = kyle
password = 123
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
Error message
"Warning: session_start() [function.session-start]: Cannot send session cookie - headers already sent by (output started at /home/bvghy/public_html/whatsmyowncarworth.com/login/login2.php:8) in /home/bvghy/public_html/whatsmyowncarworth.com/login/login2.php on line 10
Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at /home/bvghy/public_html/whatsmyowncarworth.com/login/login2.php:8) in /home/bvghy/public_html/whatsmyowncarworth.com/login/login2.php on line 10"
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
login2.php page = php code
<?php
session_start();
$username = $_POST['username'];
$password = $_POST['password'];
if ($username&&$password)
{
$connect = mysql_connect("localhost","bvghy_bvghy","mypassword") or die("Counldn't Connect!");
mysql_select_db("bvghy_phplogin") or die("Counldn't Connect to DB");
$query = mysql_query("SELECT id, username, password FROM users WHERE username='$username'");
$numrows = mysql_num_rows($query);
if ($numrows!=0)
{
// code to login
// The $row is a varialbe of mysql_fetch_assoc. The $row fetches the the above query. $dbusername and $dbpassword are varialbes of $row['username']; and $row['password'];
// the 'username' and 'password' are references to the rows in the MySql database
while ($row = mysql_fetch_assoc($query))
{
$dbusername = $row['username'];
$dbpassword = $row['password'];
}
// check to see if they match!
if ($username==$dbusername&&$password==$dbpassword)
{
echo "Your in! <a href='member.php'>Click</a> here to enter the member page";
$_SESSION['username']=$dbusername;
}
else
echo "Incorrect password!";
}
else
die("That user dosen't exist!");
I'm new to this forum. They is my first post and I'm currently learning how to code via youtube videos!
I'm trying to create a session_start(); for my website. Every time I login I'm getting a nasty error message and not sure why.
Would you guys happen to know what I'm doing wrong? I don't think I'm mistyping session_start(); incorrectly. Do I need to declare something in the session? I'm confused ......
Thanks everyone in advance!
http://whatsmyowncarworth.com/login/login.php
User info
username = alex
password = abc
username = kyle
password = 123
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
Error message
"Warning: session_start() [function.session-start]: Cannot send session cookie - headers already sent by (output started at /home/bvghy/public_html/whatsmyowncarworth.com/login/login2.php:8) in /home/bvghy/public_html/whatsmyowncarworth.com/login/login2.php on line 10
Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at /home/bvghy/public_html/whatsmyowncarworth.com/login/login2.php:8) in /home/bvghy/public_html/whatsmyowncarworth.com/login/login2.php on line 10"
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
login2.php page = php code
<?php
session_start();
$username = $_POST['username'];
$password = $_POST['password'];
if ($username&&$password)
{
$connect = mysql_connect("localhost","bvghy_bvghy","mypassword") or die("Counldn't Connect!");
mysql_select_db("bvghy_phplogin") or die("Counldn't Connect to DB");
$query = mysql_query("SELECT id, username, password FROM users WHERE username='$username'");
$numrows = mysql_num_rows($query);
if ($numrows!=0)
{
// code to login
// The $row is a varialbe of mysql_fetch_assoc. The $row fetches the the above query. $dbusername and $dbpassword are varialbes of $row['username']; and $row['password'];
// the 'username' and 'password' are references to the rows in the MySql database
while ($row = mysql_fetch_assoc($query))
{
$dbusername = $row['username'];
$dbpassword = $row['password'];
}
// check to see if they match!
if ($username==$dbusername&&$password==$dbpassword)
{
echo "Your in! <a href='member.php'>Click</a> here to enter the member page";
$_SESSION['username']=$dbusername;
}
else
echo "Incorrect password!";
}
else
die("That user dosen't exist!");