Jump to content


Photo

Variable question


  • Please log in to reply
24 replies to this topic

#1 fat_bob

fat_bob
  • Members
  • PipPip
  • Member
  • 12 posts

Posted 30 July 2006 - 10:01 AM

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. ;)

#2 hostfreak

hostfreak
  • Members
  • PipPipPip
  • Advanced Member
  • 581 posts

Posted 30 July 2006 - 10:09 AM

http://www.php.net/global -I'm pretty sure thats what your looking for

#3 fat_bob

fat_bob
  • Members
  • PipPip
  • Member
  • 12 posts

Posted 30 July 2006 - 10:31 AM

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)
<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>

This is the php that the form is linked to.
<?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;

}
?>

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!

#4 hackerkts

hackerkts
  • Members
  • PipPipPip
  • Advanced Member
  • 593 posts
  • LocationSingapore
  • Age:18

Posted 30 July 2006 - 10:37 AM

You can use session, at the top of the script put this
<?php
session_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]


Regards,
hackerkts

To be a coder, you must learn how to think and not to give up so easily.


#5 king arthur

king arthur
  • Members
  • PipPipPip
  • Advanced Member
  • 335 posts
  • LocationUK HQ

Posted 30 July 2006 - 10:40 AM

Sounds like what you want is to use sessions.

Right at the start of your script, put

session_start();

Then instead of
if($userdb == $userfo and $passdb == $passfo){

$loger = 1;

}else{

$loger = 0;

have

if($userdb == $userfo and $passdb == $passfo){

$_SESSION["loger"] = 1;

}else{

$_SESSION["loger"] = 0;

Then in the other page you can do:

session_start();

if(!isset($_SESSION["loger"])) $loger = $_SESSION["loger"];

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.
Sir Isaac Newton said "If I have seen farther, it is by standing on the shoulders of giants". But it is not recorded as to whether he said it before or after he was hit on the head by a falling apple.

#6 king arthur

king arthur
  • Members
  • PipPipPip
  • Advanced Member
  • 335 posts
  • LocationUK HQ

Posted 30 July 2006 - 10:40 AM

Damn, have to learn to type faster. ;)
Sir Isaac Newton said "If I have seen farther, it is by standing on the shoulders of giants". But it is not recorded as to whether he said it before or after he was hit on the head by a falling apple.

#7 hackerkts

hackerkts
  • Members
  • PipPipPip
  • Advanced Member
  • 593 posts
  • LocationSingapore
  • Age:18

Posted 30 July 2006 - 10:49 AM

Hahaz lol :P

Edit:
You can also try to use cookie.

Regards,
hackerkts

To be a coder, you must learn how to think and not to give up so easily.


#8 fat_bob

fat_bob
  • Members
  • PipPip
  • Member
  • 12 posts

Posted 30 July 2006 - 10:54 AM

Do you mean that I should do like this on the new page if I want it to print the value of the variable?

<?php
session_start();
echo $_SESSION['loger'];

?>

cause it doesnt seams to find the variable.

#9 hackerkts

hackerkts
  • Members
  • PipPipPip
  • Advanced Member
  • 593 posts
  • LocationSingapore
  • Age:18

Posted 30 July 2006 - 10:59 AM

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
<?php
session_start();
$loger = $_SESSION['loger'];
if ($loger == 1) {
   //logged in
} else {
   //ask them to login
}
?>


Regards,
hackerkts

To be a coder, you must learn how to think and not to give up so easily.


#10 fat_bob

fat_bob
  • Members
  • PipPip
  • Member
  • 12 posts

Posted 30 July 2006 - 11:10 AM

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
<?php
session_start();
$loger = $_SESSION['loger'];
if ($loger == 1) {
   //logged in
} else {
   //ask them to login
}
?>
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.
<?php
session_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;

}
?>

The page that the link "links" the user to once its pressed.
<?php
session_start();
$loger = $_SESSION['loger'];
if ($loger == 1) {
   echo "Succes!";
} else {
   echo "Error!";
}
?>

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.

#11 hackerkts

hackerkts
  • Members
  • PipPipPip
  • Advanced Member
  • 593 posts
  • LocationSingapore
  • Age:18

Posted 30 July 2006 - 11:19 AM

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.

Regards,
hackerkts

To be a coder, you must learn how to think and not to give up so easily.


#12 fat_bob

fat_bob
  • Members
  • PipPip
  • Member
  • 12 posts

Posted 30 July 2006 - 11:23 AM

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 3

Notice: Undefined variable: loger in ...\test.php on line 4
bajs

#13 hackerkts

hackerkts
  • Members
  • PipPipPip
  • Advanced Member
  • 593 posts
  • LocationSingapore
  • Age:18

Posted 30 July 2006 - 11:27 AM

Weird, can't be
Try
$_SESSION['loger'] = $loger;


Regards,
hackerkts

To be a coder, you must learn how to think and not to give up so easily.


#14 king arthur

king arthur
  • Members
  • PipPipPip
  • Advanced Member
  • 335 posts
  • LocationUK HQ

Posted 30 July 2006 - 11:32 AM

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 3

Notice: Undefined variable: loger in ...\test.php on line 4
bajs


What version of PHP are you using?
Sir Isaac Newton said "If I have seen farther, it is by standing on the shoulders of giants". But it is not recorded as to whether he said it before or after he was hit on the head by a falling apple.

#15 fat_bob

fat_bob
  • Members
  • PipPip
  • Member
  • 12 posts

Posted 30 July 2006 - 11:34 AM

Nope....didnt solve the case.... :(

#16 fat_bob

fat_bob
  • Members
  • PipPip
  • Member
  • 12 posts

Posted 30 July 2006 - 11:35 AM

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

#17 hackerkts

hackerkts
  • Members
  • PipPipPip
  • Advanced Member
  • 593 posts
  • LocationSingapore
  • Age:18

Posted 30 July 2006 - 11:38 AM

Save this at a new page
<?=phpinfo();?>

Then you see the top, it will show something like PHP Version 5.1.4

Regards,
hackerkts

To be a coder, you must learn how to think and not to give up so easily.


#18 fat_bob

fat_bob
  • Members
  • PipPip
  • Member
  • 12 posts

Posted 30 July 2006 - 11:42 AM

PHP Version 4.3.10

sounds very old... :-\

#19 hackerkts

hackerkts
  • Members
  • PipPipPip
  • Advanced Member
  • 593 posts
  • LocationSingapore
  • Age:18

Posted 30 July 2006 - 11:45 AM

4.3.10 shouldn't have any problem.
Try save this at the new file and see what it says.
<?php
session_start();
$_SESSION['test'] = "I'm a SESSION";
echo $_SESSION['test'];
?>

You should see I'm a SESSION.

Regards,
hackerkts

To be a coder, you must learn how to think and not to give up so easily.


#20 fat_bob

fat_bob
  • Members
  • PipPip
  • Member
  • 12 posts

Posted 30 July 2006 - 11:49 AM

Yes I do see the text.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users