gk20 Posted March 15, 2006 Share Posted March 15, 2006 I have a php file where I allow users that are logged in to add to the database.I am using session_start() and session_checker() to see of the user is logged in before allowing them access to this page. That works fine. But when I add their info to the database I also want to add their username with it, to associate this informtion with them without asking them for it again??? Is there anyway I could use their loginname from login.php in the placead.php file?Heres login.php$login = $_POST['login'];$pass = $_POST['pass'];......session_register('login');$_SESSION['loginname'] = $login;session_register('pass');$_SESSION['password'] = $pass;heres placead.php$itemname = $_POST['itemname'];$description = $_POST['description'];$category = $_POST['category'];$cost = $_POST['cost'];$location = $_POST['location'];when I'm storing these values can I how can I store the username of that person with the information??Please help, I've tried looking in to sessions, but I can't seem to understand it!!! Quote Link to comment Share on other sites More sharing options...
komquat Posted March 15, 2006 Share Posted March 15, 2006 [!--quoteo(post=355333:date=Mar 15 2006, 08:10 AM:name=gk20)--][div class=\'quotetop\']QUOTE(gk20 @ Mar 15 2006, 08:10 AM) [snapback]355333[/snapback][/div][div class=\'quotemain\'][!--quotec--]I have a php file where I allow users that are logged in to add to the database.I am using session_start() and session_checker() to see of the user is logged in before allowing them access to this page. That works fine. But when I add their info to the database I also want to add their username with it, to associate this informtion with them without asking them for it again??? Is there anyway I could use their loginname from login.php in the placead.php file?Heres login.php$login = $_POST['login'];$pass = $_POST['pass'];......session_register('login');$_SESSION['loginname'] = $login;session_register('pass');$_SESSION['password'] = $pass;heres placead.php$itemname = $_POST['itemname'];$description = $_POST['description'];$category = $_POST['category'];$cost = $_POST['cost'];$location = $_POST['location'];when I'm storing these values can I how can I store the username of that person with the information??Please help, I've tried looking in to sessions, but I can't seem to understand it!!![/quote]You start placead.php withSession_start()Then you can call up the variable $_SESSION['loginname'] anytime after that and it will give you that value. Quote Link to comment Share on other sites More sharing options...
gk20 Posted March 15, 2006 Author Share Posted March 15, 2006 Sorry, I have tried this. In my placead.php file i have this at top<?php include 'db.inc.php'; ?><?php session_start(); session_checker();?>..............session_register('loginname');$itemowner = $_SESSION['loginname'];so when I'm adding to database: insert into item set ..... itemowner = '$itemowner' but it still leaves itemowner field blank!would it have anything to do with db.inc.php file??......function session_checker(){ if(!session_is_registered('loginname')) { if(!session_is_registered('loginpass')) { echo "<p>You must be logged in to view this page</p>"; echo "<b><br><br/><a href=login.php?loginuser=1>Login</a></b>"; exit(); } }}Its really bugging me, it should be easier!!! Quote Link to comment Share on other sites More sharing options...
redarrow Posted March 15, 2006 Share Posted March 15, 2006 Try this way ok.If session_start () is not at the very top of a page with no text above it should work, i say agin no text above it.Also chage this okinclude ('db.inc.php');To this save page to database.phpinclude ('database.php');good luck.[code]<?php session_start();session_checker();// this needs to be saved as database.php not what you had.include ('database.php');//session_register('loginname');$itemowner = $_SESSION['loginname'];//so when I'm adding to database: insert into item set ..... $itemowner = '$itemowner' //but it still leaves itemowner field blank!//would it have anything to do with db.inc.php file??function session_checker(){ if(!session_is_registered('loginname')){if(!session_is_registered('loginpass')) { echo "<p>You must be logged in to view this page</p>";echo "<b><br><br/><a href=login.php?loginuser=1>Login</a></b>"; exit(); }}}?>[/code] Quote Link to comment Share on other sites More sharing options...
redarrow Posted March 15, 2006 Share Posted March 15, 2006 PHP FREAKS EXAMPLE FOR YOUJUST CHANGE THE DATABASE TO YOUR SETTINGS OKGOOD LUCK. [code]<?php ################ # Simple login session # Ben M Wasley # BenWConsulting.com ################ function login($statment) { print" <form action="myscript.php" method="POST">n"; print"<div align="center"><b>".$statment."</b></div><br />n"; //Your login vars user &amp; password need to be passed no form tags though look above &amp; below //Close form print"</form>n"; } //Duh start the session redundant comments are funny session_start(); if(!isset($user) | !isset($password)) { //Login message here $statment = "Hi, Please login below"; login($statment); exit(); } session_register("user"); session_register("password"); //However you connect to your DB include("db.php"); $sql = mysql_query("SELECT user, password FROM users WHERE user = '$user'"); $result = mysql_fetch_array($sql)or die ("Doh! no DB Connection"); $numrows = mysql_num_rows($sql); if($numrows != "0" & $password == $result["password"]) { $valid_user = 1; } else { $valid_user = 0; } mysql_close($db); if (!($valid_user)) { //Boot em!!! IF !valid session_unset(); session_destroy(); //Give an error message $statment = "Somthing is wrong with the info you submitted.n"; login($statment); //Stop script exit(); } //Include session_start(); on the top of all pages or in your header.php include session_start(); //Wecome to the the protected area!!! Put HTML, include, whatever you want here. //Session will end when client closes browser. //If you want then to have the ability to logout use: // ---> session_start(); // ---> session_unset(); // ---> session_destroy(); //@ the top of any page or as a seperate script i.e logout.php ?> [/code] 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.