Jump to content

login


searls03

Recommended Posts

 

ok so here is the code:

<?php
session_start(); // Must start session first thing
/* 
Created By Adam Khoury @ www.flashbuilding.com 
-----------------------June 20, 2008----------------------- 
*/
// Here we run a login check
include_once "connect.php";
// Place Session variable 'id' into local variable


$sql = mysql_query("SELECT * FROM cart");
while($row = mysql_fetch_array($sql)){
$cid = $row["cart_id"];
$academy = $row["academy"];
$complete = "".$cid."".$academy."";
$_SESSION['complete'] = $complete;

$_SESSION['cart_id'] = $cid;
$_SESSION['academy'] = $academy;

}
if (!isset($_SESSION['complete'])) { 
   echo 'Please <a href="/login.php">log in</a> to access your account';
   exit(); 
}

//Connect to the database through our include 
include_once "connect.php";
// Place Session variable 'id' into local variable
$academy = $_SESSION['academy'];


$sql = mysql_query("SELECT * FROM cart");
while($row = mysql_fetch_array($sql)){
$cid = $row["cart_id"];
$_SESSION['cart_id'] = $cid;

}

echo $cid;
echo "<br />";
echo $academy;


?>

 

what I need to know how to do is arrange my code so that if the person is logged in already, the part before the if isnot set, will run.  but if the person is not already logged in, they must login first.  the problem is if one person is logged in, all people can get logged in.  please help....

 

 

 

I may not be setting them right either.  :

<?php
/*
Created By Adam Khoury @ [url=http://'http://www.flashbuilding.com/']www.flashbuilding.com[/url]
-----------------------June 20, 2008-----------------------
*/
if ($_POST['username']) {
//Connect to the database through our include
include_once "connect.php";
$username = stripslashes($_POST['username']);
$username = strip_tags($username);
$username = mysql_real_escape_string($username);
$password = ereg_replace("[^A-Za-z0-9]", "", $_POST['password']);
$password = md5($password); // filter everything but numbers and letters
// Make query and then register all database data that -
// cannot be changed by member into SESSION variables.
// Data that you want member to be able to change -
// should never be set into a SESSION variable.
$sql = mysql_query("SELECT * FROM users WHERE username='$username' AND password='$password'");
$login_check = mysql_num_rows($sql);
if($login_check > 0){
while($row = mysql_fetch_array($sql)){
// Get member ID into a session variable
$academy = $row["academy"];

$_SESSION['academy'] = $academy;
$_SESSION['logged'] = "1";

function genRandomString($length = 20) {
     $characters = '0123456789';
     $string ='';    


    for ($p = 0; $p < $length; $p++) {
         $string .= $characters[mt_rand(0, strlen($characters))];
     }

    return $string;

}

$bar =  genRandomString();


$sql = "INSERT INTO cart (cart_id, academy) 
VALUES('".$bar."".$academy."','$academy' )";
$rs = mysql_query($sql) or die ("Problem with the query: $sql <br />" . mysql_error());
$id = $bar;

$_SESSION['cart_id'] = $cid;
$_SESSION['complete'] = "".$cid."".$academy."";

// Get member username into a session variable

$errorMsg = '';
$pass = '';
$remember = '';
if (isset($_POST['Submit'])) {

$pass = $_POST['password'];
if (isset($_POST['remember'])) {
	$remember = $_POST['remember'];
}
$pass = stripslashes($pass);
$pass = strip_tags($pass);
if($remember == "yes"){
                    $encryptedID = base64_encode("g4enm2c0c4y3dn3727553$id");
    			    setcookie("idCookie", $encryptedID, time()+60*60*24*100, "/"); // Cookie set to expire in about 30 days
		        setcookie("passCookie", $password, time()+60*60*24*100, "/"); // Cookie set to expire in about 30 days
    			} 
    			// All good they are logged in, send them to homepage then exit script
    			header("location: index.php"); 
exit();
}} // close while
} else {
// Print login failure message to the user and link them back to your login page
print '<br /><br /><font color="#FF0000">Sorry, incorrect username or password, please try again! </font><br />';

}}
?>

 

It seems that I have to query the database on every page for them to actually work...why is this?  I don't beleive they are setting....like I echo logged, but nothing shows......so I don't think they are working.

 

Link to comment
Share on other sites

searls03,

 

Will this work for you ?

 


session_start(); 

// Must start session first thing 
/* Created By Adam Khoury @ www.flashbuilding.com 
-----------------------June 20, 2008----------------------- 
*/

// Here we run a login check



if (!isset($_SESSION['complete'])) {    
echo 'Please <a href="/login.php">log in</a> to access your account';   exit(); 
}
else {

// Place Session variable 'id' into local variable
require_once "connect.php";

$sql = mysql_query("SELECT * FROM cart");

while($row = mysql_fetch_array($sql)){
	$cid = $row["cart_id"];
	$academy = $row["academy"];
	$complete = "".$cid."".$academy."";
	$_SESSION['complete'] = $complete;
	$_SESSION['cart_id'] = $cid;
	$_SESSION['academy'] = $academy;
}

}

//Connect to the database through our include 

require_once "connect.php";// Place Session variable 'id' into local variable

$academy = $_SESSION['academy'];
$sql = mysql_query("SELECT * FROM cart");

while($row = mysql_fetch_array($sql)){
$cid = $row["cart_id"];
$_SESSION['cart_id'] = $cid;
}

echo $cid;
echo "<br />";
echo $academy;

?>

 

I changed 'include_once' to 'require_once' so the prcess will die if your SQL credentials are not found.

 

Scot L. Diddle, Richmond VA

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.