Jump to content

Archived

This topic is now archived and is closed to further replies.

fat_bob

Variable question

Recommended Posts

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

Share this post


Link to post
Share on other sites
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!

Share this post


Link to post
Share on other sites
You can use session, at the top of the script put this
[code]
<?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][/code]

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Hahaz lol :P

[b]Edit[/b]:
You can also try to use cookie.

Share this post


Link to post
Share on other sites
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]<?php
session_start();
echo $_SESSION['loger'];

?>[/code]

cause it doesnt seams to find the variable.

Share this post


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

Share this post


Link to post
Share on other sites
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]<?php
session_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]<?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;

}
?>[/code]

The page that the link "links" the user to once its pressed.
[code]
<?php
session_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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
Weird, can't be
Try
[code]$_SESSION['loger'] = $loger;[/code]

Share this post


Link to post
Share on other sites
[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 3

Notice: Undefined variable: loger in ...\test.php on line 4
bajs
[/quote]

What version of PHP are you using?

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
Save this at a new page
[code]<?=phpinfo();?>[/code]

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

Share this post


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

You should see [b]I'm a SESSION[/b].

Share this post


Link to post
Share on other sites
But in my other page the
<?php
session_start();
$_SESSION['test'] = "I'm a SESSION";

part.
and the

echo $_SESSION['test'];
?>

aint in the same page.

Share this post


Link to post
Share on other sites
You have the session_start() at the top of each page where you are using session vaiables?

Share this post


Link to post
Share on other sites
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^^)

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.