...

View Full Version : Sessions are not registering.



MHaris
08-17-2007, 12:26 AM
<?php

// Last Modified 16th August

ob_start();

include('templates/header.php'); // Header template
include('../config/config.php'); // Includes configuration files
include('../lib/functions.php'); // Load all functions from Library

include('templates/login.php'); // Login form

$submit = $_POST['log_in'];
$posted_username = sql_safe($_POST['username']);
$posted_password = sql_safe($_POST['password']);

if(isset($submit)){

if($posted_username == $username && $posted_password == $password){
echo "Logged in";
$_SESSION["username"];
$_SESSION["password"];
}
else {
echo "Wrong username or password";
}

}

include('templates/footer.php'); // Footer Template

?>

Sessions are not registering for me. An example of sessions being used at are:


<?php

// Last Modified 14th August 2007
ob_start();
session_start(); // Loads session

include('templates/header.php'); // Header Template
include('../config/config.php'); // Includes configuration files
include('../lib/functions.php'); // Load all functions from Library

if(!isset($_SESSION["username"])){
header('Location: index.php');
exit();
}
else {

include('templates/candidates.php'); // Add new candidates template

$submit = $_POST['submit']; // Submit button
$candidate = sql_safe($_POST['name']); // New candidate's name

if(isset($submit)){

mysql_query("INSERT INTO candidates(name) VALUES('$candidate')") or die(mysql_error());

$newly_created_candidate = mysql_query("SELECT * FROM candidates ORDER BY `id` desc LIMIT 1");
while ($row = mysql_fetch_array($newly_created_candidate)){
$candidateID = $row['id'];
}

$result = mysql_query("SELECT id FROM questions");
while ($row = mysql_fetch_array($result, MYSQL_NUM)) {
$qid=$row[0];
$query = "INSERT INTO answers(answer,candidate_id,question_id) VALUES('1','$candidateID','$qid')";
mysql_query($query)or die(mysql_error());
}

}

$sql = mysql_query("SELECT * FROM candidates");
while($row = mysql_fetch_array($sql)){
echo $row['name'];
echo "<br />";
$count++;
}

echo '<br /><b>There are '. $count.' Candidates</b>';

mysql_close(); // Closes DB connection
}

include('templates/footer.php'); // Footer Template

?>

Any ideas, what am I doing wrong?

PappaJohn
08-17-2007, 12:40 AM
You never used session_start() in the first code, and I don't know what these lines are supposed to be doing:


$_SESSION["username"];
$_SESSION["password"];

MHaris
08-17-2007, 12:50 AM
You never used session_start() in the first code, and I don't know what these lines are supposed to be doing:


$_SESSION["username"];
$_SESSION["password"];



Where do I start the session? It doesn't work.


$_SESSION["username"];
$_SESSION["password"];

These lines are suppose to be registering sessions?

PappaJohn
08-17-2007, 12:53 AM
session_start() should be at the top of the first script, like it is in the second.

In order to set session variables, you have to set them to something - like this:


$_SESSION['username'] = 'my name';
$_SESSION['password'] = 'my password';

this has to be done after session_start()

On a side note, it's probably not a good idea to be storing passwords in sessions variables.

MHaris
08-17-2007, 12:57 AM
session_start() should be at the top of the first script, like it is in the second.

In order to set session variables, you have to set them to something - like this:


$_SESSION['username'] = 'my name';
$_SESSION['password'] = 'my password';

this has to be done after session_start()

On a side note, it's probably not a good idea to be storing passwords in sessions variables.

I can store them in a database but the script only needs basic functions to login.

Thanks anyways, fixed.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum