session variables - PHP retrieving username of current user on page and then using it in a sql query -
i making website user has log in/create account , can type in terms search on twitter on own personal dashboard.
to make sure logged in on every page have code:
<?php session_start(); if (isset($_session['loggedin'])) { $_session["username"] = $username; echo "(you logged in)"; } else { echo "(not logged in)"; echo"<p><a href = 'login.php'>log in</a>"; echo"<p><a href = 'createaccount.php'>create account</a>"; exit(); } ?>
in code try , take username logged in login page
<?php session_start(); //connect database $db = mysqli_connect("number", "name", "password"); //msg if not connected if (!$db) { echo "sorry!i can't connect database"; } if(isset($_post['submit'])) { $username =$_post['username']; $password = $_post['password']; if(empty($username) or empty ($password)) { echo "you missed something"; } else { if(!empty($username) && !empty($password)) { mysqli_select_db ($db,"name"); $qry=" select username login username= '$username' , password = '$password';"; $result = mysqli_query($db,$qry); $num_rows = mysqli_num_rows($result); if(($num_rows) == 1) { //username successful header("location:index.php"); $_session['loggedin'] = 'yes'; $username = $_session['username']; } else { //username failed print '<script type="text/javascript">'; print 'alert("incorrect infomation. try again.")'; print '</script>'; } } } } ?>
and on dashboards enter terms want can't figure out how know logged in there (im trying term 1 atm)
<?php session_start(); //connect database $db = mysqli_connect("number", "name", "password"); //msg if not connected if (!$db) { echo "sorry!i can't connect database"; } $term1 =$_post['term1']; $term2 = $_post['term2']; $term3 = $_post['term3']; $_session["username"] = $username; if(isset($_post['submit'])) { mysqli_select_db ($db,"sarahpattison"); $query2 = "insert terms (term) values ('$term1') ;"; $result = mysqli_query($db,$query2); $term1id = mysqli_insert_id($db); echo $term1id; if ($result){ $qry5="insert userterms(userid,termid) select login.userid, terms.termid login, terms login.username = '$username' , terms.termid='$term1id';"; $result8 = mysqli_query($db,$qry5); } } ?>
the $username variable isn't working , im wondering shouldn't have checklogin part different page. don't know session variables.
try
$username = $_session["username"];
instead of
$_session["username"] = $username;
in third file
Comments
Post a Comment