searls03 Posted February 16, 2012 Share Posted February 16, 2012 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. Quote Link to comment Share on other sites More sharing options...
ScotDiddle Posted February 16, 2012 Share Posted February 16, 2012 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 Quote Link to comment Share on other sites More sharing options...
searls03 Posted February 16, 2012 Author Share Posted February 16, 2012 I am pretty sure the sessions aren't setting.....when I go to index.php and try to echo them out, nothing shows.... Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.