fat_bob Posted July 30, 2006 Share Posted July 30, 2006 Hi!I have a question, you see I want to take an already existing variable on a diffrent page and then call that variable in another page aswell, but I only want the variable and not the whole code, so the include() doesnt work as far as I can notice.And I also want to avoid to use a database for this since I want the value of the variable to be added to the local user machine and not in a server.Is this possible?please help me!If you need the code to understand what I mean then just ask. ;) Quote Link to comment Share on other sites More sharing options...
hostfreak Posted July 30, 2006 Share Posted July 30, 2006 http://www.php.net/global -I'm pretty sure thats what your looking for Quote Link to comment Share on other sites More sharing options...
fat_bob Posted July 30, 2006 Author Share Posted July 30, 2006 Close but not exactly what I wanted..I think.I will let you have my litle code aswell, so I can explain my request abit better.This is a form that where the user puts in there username and password which is "linked" to another php page(see belowe)[code]<html><head> <title>Personal INFO</title> </head> <body> <form method="post" action="dbsearch.php"> user:<input type="text" size="12" maxlength="12" name="user1"><br /> password:<input type="text" size="12" maxlength="12" name="password1"><br /> <input type="submit" value="submit" name="logg in"></form> </body></html>[/code]This is the php that the form is linked to.[code]<?php$con = mysql_connect("localhost","root","");if (!$con) { die('Could not connect: ' . mysql_error()); }mysql_select_db("untitled", $con);$result = mysql_query("SELECT * FROM userlogin");while($row = mysql_fetch_array($result)) {$userdb = $row['user'];$passdb = $row['password'];}mysql_close($con);$userfo = $_POST["user1"];$passfo = $_POST["password1"];if($userdb == $userfo and $passdb == $passfo){$loger = 1;}else{$loger = 0;}?>[/code]Now I want to make a completly new php page but I want to get the value of the "loger" variable(s) in the new page from the old page above. so if the user on the last page did enter $userdb == $userfo and $passdb == $passfo right then the new page that I want to create should call the variable $loger with a value of "1", and if $userdb == $userfo and $passdb == $passfo was flase then the variable should have a value of "0".The problem is that I dont want to use and database or call any other inc files or so.Is this possible or will I have to make it another way?and please now you have the code. so if you have any ideas give me the full code,since im quite new to php so that whould make it much easier for me to understand and etc.Thanks! Quote Link to comment Share on other sites More sharing options...
hackerkts Posted July 30, 2006 Share Posted July 30, 2006 You can use session, at the top of the script put this[code]<?phpsession_start();?>[code]Then change[code]$loger = 1;[/code]To[code]$_SESSION['loger'] = 1;[/code]And also, change[code]$loger = 0;[/code]To[code]$_SESSION['loger'] = 0;[/code]So on the other page, you can check if it is 0 or 1 by using $_SESSION['loger'].Btw.. You have to put the [b]session_start();[/b] on the new page also.[/code][/code] Quote Link to comment Share on other sites More sharing options...
king arthur Posted July 30, 2006 Share Posted July 30, 2006 Sounds like what you want is to use sessions.Right at the start of your script, put[code]session_start();[/code]Then instead of[code]if($userdb == $userfo and $passdb == $passfo){$loger = 1;}else{$loger = 0;[/code]have[code]if($userdb == $userfo and $passdb == $passfo){$_SESSION["loger"] = 1;}else{$_SESSION["loger"] = 0;[/code]Then in the other page you can do:[code]session_start();if(!isset($_SESSION["loger"])) $loger = $_SESSION["loger"];[/code]If the $loger viariable isn't set then obviously someone came to this page without going to the other page first. Always put the session_start() right at the top of the script. Quote Link to comment Share on other sites More sharing options...
king arthur Posted July 30, 2006 Share Posted July 30, 2006 Damn, have to learn to type faster. ;) Quote Link to comment Share on other sites More sharing options...
hackerkts Posted July 30, 2006 Share Posted July 30, 2006 Hahaz lol :P[b]Edit[/b]:You can also try to use cookie. Quote Link to comment Share on other sites More sharing options...
fat_bob Posted July 30, 2006 Author Share Posted July 30, 2006 Do you mean that I should do like this on the new page if I want it to print the value of the variable?[code]<?phpsession_start();echo $_SESSION['loger'];?>[/code]cause it doesnt seams to find the variable. Quote Link to comment Share on other sites More sharing options...
hackerkts Posted July 30, 2006 Share Posted July 30, 2006 Yes, this is how you echo them out.If you want to check if the user loged in on the new page you can do this[code]<?phpsession_start();$loger = $_SESSION['loger'];if ($loger == 1) { //logged in} else { //ask them to login}?>[/code] Quote Link to comment Share on other sites More sharing options...
fat_bob Posted July 30, 2006 Author Share Posted July 30, 2006 Well the thing is that if the user enters the right name and pass they will get a link that they press on. and when they press on that link they get linked to a blank new page with this code atm[code]<?phpsession_start();$loger = $_SESSION['loger'];if ($loger == 1) { //logged in} else { //ask them to login}?>[/code]that you posted to me. But it doesnt seams as the variable $loger exist anyway. Does this comand store the variable on the computer memory until I want it to get deleted or is it just there as long as the page with the variable fuction is loaded?.Once again here you have the full code. but the "from" thing is still the same.The page that creates the variables and checks if the user name and pass is right and the prints a link depending on the users name and pass is right or wrong.[code]<?phpsession_start();$con = mysql_connect("localhost","root","");if (!$con) { die('Could not connect: ' . mysql_error()); }mysql_select_db("untitled", $con);$result = mysql_query("SELECT * FROM userlogin");while($row = mysql_fetch_array($result)) {$userdb = $row['user'];$passdb = $row['password'];}mysql_close($con);$userfo = $_POST["user1"];$passfo = $_POST["password1"];if($userdb == $userfo and $passdb == $passfo){$_SESSION['loger'] = 1;echo "<a href='test.php'>test</a>";echo $_SESSION['loger'];}else{$_SESSION['loger'] = 0;}?>[/code]The page that the link "links" the user to once its pressed.[code]<?phpsession_start();$loger = $_SESSION['loger'];if ($loger == 1) { echo "Succes!";} else { echo "Error!";}?>[/code]Thanks for trying to help me btw...even thoe im noob^^...The best thing whould be if there was some way to create a variable that stores on the compluter memory, and that you can call anytime during the session thats the computer is turned on and once its shutdown the variable delets it self, and there also is some sort of "delete" function so you can for example press on a button on the web site (example "log out") and then the stored variable get erased from the computers memory. Quote Link to comment Share on other sites More sharing options...
hackerkts Posted July 30, 2006 Share Posted July 30, 2006 session is stored until the user open new window (that's what I heard from others).Session is just like cookie, it will keep those information until the user clear his cookie or the cookie expired. Quote Link to comment Share on other sites More sharing options...
fat_bob Posted July 30, 2006 Author Share Posted July 30, 2006 Very strange.....since it says that it cant find the variable and the index or something.hmm :-[ ???Notice: Undefined index: loger in ...\test.php on line 3Notice: Undefined variable: loger in ...\test.php on line 4bajs Quote Link to comment Share on other sites More sharing options...
hackerkts Posted July 30, 2006 Share Posted July 30, 2006 Weird, can't beTry [code]$_SESSION['loger'] = $loger;[/code] Quote Link to comment Share on other sites More sharing options...
king arthur Posted July 30, 2006 Share Posted July 30, 2006 [quote author=fat_bob link=topic=102316.msg405879#msg405879 date=1154258625]Very strange.....since it says that it cant find the variable and the index or something.hmm :-[ ???Notice: Undefined index: loger in ...\test.php on line 3Notice: Undefined variable: loger in ...\test.php on line 4bajs[/quote]What version of PHP are you using? Quote Link to comment Share on other sites More sharing options...
fat_bob Posted July 30, 2006 Author Share Posted July 30, 2006 Nope....didnt solve the case.... :( Quote Link to comment Share on other sites More sharing options...
fat_bob Posted July 30, 2006 Author Share Posted July 30, 2006 haha no idea king....as I said im noob^^ but if you mean what sort of "server" im runing the script on, then its the latest verion of easyPHP Quote Link to comment Share on other sites More sharing options...
hackerkts Posted July 30, 2006 Share Posted July 30, 2006 Save this at a new page[code]<?=phpinfo();?>[/code]Then you see the top, it will show something like PHP Version 5.1.4 Quote Link to comment Share on other sites More sharing options...
fat_bob Posted July 30, 2006 Author Share Posted July 30, 2006 PHP Version 4.3.10sounds very old... :-\ Quote Link to comment Share on other sites More sharing options...
hackerkts Posted July 30, 2006 Share Posted July 30, 2006 4.3.10 shouldn't have any problem.Try save this at the new file and see what it says.[code]<?phpsession_start();$_SESSION['test'] = "I'm a SESSION";echo $_SESSION['test'];?>[/code]You should see [b]I'm a SESSION[/b]. Quote Link to comment Share on other sites More sharing options...
fat_bob Posted July 30, 2006 Author Share Posted July 30, 2006 Yes I do see the text. Quote Link to comment Share on other sites More sharing options...
fat_bob Posted July 30, 2006 Author Share Posted July 30, 2006 But in my other page the <?phpsession_start();$_SESSION['test'] = "I'm a SESSION";part.and theecho $_SESSION['test'];?>aint in the same page. Quote Link to comment Share on other sites More sharing options...
king arthur Posted July 30, 2006 Share Posted July 30, 2006 You have the session_start() at the top of each page where you are using session vaiables? Quote Link to comment Share on other sites More sharing options...
fat_bob Posted July 30, 2006 Author Share Posted July 30, 2006 Yes Quote Link to comment Share on other sites More sharing options...
fat_bob Posted July 30, 2006 Author Share Posted July 30, 2006 Ok I have now found the "problem" but havent solved it. The thing is that the variables value isnt stored once you press the link and gets to the new page. It seams as it doesnt store the value on the computers memory. (and its not wrong on my computer, its still the script btw^^) Quote Link to comment Share on other sites More sharing options...
hackerkts Posted July 30, 2006 Share Posted July 30, 2006 The session should works perfectly, nevermind.. Let's try cookie now.[code]<?php$con = mysql_connect("localhost","root","");if (!$con) { die('Could not connect: ' . mysql_error()); }mysql_select_db("untitled", $con);$result = mysql_query("SELECT * FROM userlogin");while($row = mysql_fetch_array($result)) {$userdb = $row['user'];$passdb = $row['password'];}mysql_close($con);$userfo = $_POST["user1"];$passfo = $_POST["password1"];if($userdb == $userfo and $passdb == $passfo){setcookie("loger", "1", time()+3600);echo "<a href='test.php'>test</a>";}else{setcookie("loger", "0", time()+3600);}?>[/code][code]<?php$loger = $_COOKIE['loger'];if ($loger == 1) { echo "Succes!";} else { echo "Error!";}?>[/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.