Jump to content

Recommended Posts

Hi Guys,

 

i normally use cokies but have been playing around with sessions, i have this basic script:

 

login.php

 

<form action="check_login.php" method="POST" />
Username:<input type="text" name="username" />
Password:<input type="password" name="password" />
<input type="submit" value="Login" />
</form>

 

check_login.php

 

<?php

session_start();

mysql_connect("localhost", "root", "xxxxxxxx");
mysql_select_db("xxxxxxxxxxxx");

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

$sql = "SELECT * FROM users WHERE username = '$username' AND password = '$password'";
$result = mysql_query($sql); 

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

echo "Username and password combination not found.";

} else {

$row = mysql_fetch_array($result);

$_SESSION['username'] = $username;

header("members.php");

}  

?>

 

members.php

 

<?php
session_start();

if (!isset($_SESSION['username'])) {
   
//Not logged in, redirect to login page   

header("login.php");

} //If they pass that, they're logged in, and are authorized to view whatever else is in this file  

echo 'YOU ARE LOGGED IN';

?>

 

i was wanting to make the users , username the logged in variable so i could later on use it to drag info from mysql about them but it doesn't seem to get past check_login.php

 

what am i doing wrong?

 

thanks guys

 

Graham

Link to comment
https://forums.phpfreaks.com/topic/58146-solved-sessions-test-not-working/
Share on other sites

Hi John,

 

sorry do you mean like this:

 

<?php
session_start();

$_SESSION['username'];

mysql_connect("localhost", "root", "milkybar");
mysql_select_db("myliveaudition");

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

$sql = "SELECT * FROM users WHERE username = '$username' AND password = '$password'";
$result = mysql_query($sql); 

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

echo "Username and password combination not found.";

} else {

$row = mysql_fetch_array($result);

$_SESSION['username'] = $username;

header("members.php");

}  

?>

 

cheers

 

Graham

Nevermind. You started the session already ($_SESSION['username'] = $username). I overlooked that before. Sorry.

 

Solution:

check_login.php:

<?php

session_start();

mysql_connect("localhost", "root", "xxxxxxxx");
mysql_select_db("xxxxxxxxxxxx");

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

$sql = "SELECT * FROM users WHERE username = '$username' AND password = '$password'";
$result = mysql_query($sql); 

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

echo "Username and password combination not found.";

} else {
    while($row = mysql_fetch_array($result)) {
        $_SESSION['username'] = $row['username'];
    }
header("members.php");

}  

?>

Hi John,

 

thanks for that, it seems to stick at check_login.php. it doesn't goto members.php (even though everything looks good it must have found the correct credentials or it would have said otherwise)

 

is there something i have overlooked?

 

cheers

 

Graham

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.