Jump to content

Variable question


fat_bob

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. ;)
Link to comment
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!
Link to comment
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]
Link to comment
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.
Link to comment
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]
Link to comment
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.
Link to comment
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?
Link to comment
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^^)
Link to comment
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]
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.