I Cant login for some reazon, every time i open with
the testing file password exist, i have put an echo-test and mark it, but the code allways runs the else and not the if.
the kernel-login-logout.php is this:
<?php
$formusername = $_GET["username"];
$formpasswd = $_GET["passwd"];
$passwdget = file_get_contents( "users/$formusername/passwd/thepasswd.txt" );
if ($formpasswd == $passwdget)
{
setcookie("libusername", $formusername,time() + (86400 * 7));
setcookie("libpasswd", $formpasswd,time() + (86400 * 7));
}
else
{
echo ("Password not found<br>");
}
?>
<?php echo ($passwdget); ?><br>
<?php echo ($formpasswd); ?>
the url:
kernel-login-logout.php?username=userdokimi&passwd=testpasswd&login=Login
the output:
Password not found
testpasswd
testpasswd
You have code like this:
if A
if B
else C
That else applies to the if B, NOT the if A.
I think you meant this:
if A
elseif B
else C
Try this:
<?php echo "'$passwdget'",strlen($passwdget); ?><br>
<?php echo "'$formpasswd'",strlen($formpasswd); ?>
to confirm that one or both of those variables don't have some invisible characters glued on to them, making them not equal.
You have a password stored in plain text (and visible in a Query String) -- that's very insecure, but we'll let that pass for the moment.
Don't assume the keys "logout", "passwd" and "username" will always be set.
You are also missing an else there.
if (isset($_GET['passwd']) && $_GET["passwd"] == $passwdget)
{
setcookie("libusername", $username,time() + (86400 * 7));
setcookie("libpasswd", $passwd,time() + (86400 * 7));
}
else if (isset($_GET['logout']) && $_GET["logout"] == "Logout")
{
setcookie("libusername", null,time() + (86400 * 7));
setcookie("libpasswd", null,time() + (86400 * 7));
}
else
{
echo ("<br>Password not found");
}
Related
I am very new to php and this is the first time i use cookies. I am trying to create a cookie which will save user navigation inside my website. I have one page called Home, and one page called Feedback.
Problems:
When loading the webpage for the first time, php shows this error (ErrorException
Undefined index: nav_history). When page is refreshed it works properly, showing the pages name (Home or Feedback)
When navigating from Home to Feedback (or vice versa, but lets take Home to Feedback for example) the page will print Home (when it should print Feedback) and then if i refresh this page, Feedback is added below Home. I want it to show Feedback on the first try.
Code for home:
<?php
if (isset($_COOKIE['nav_history'])) {
$cookie_values = $_COOKIE["nav_history"];
$cookie_values .= "Home<br>";
setcookie("nav_history", $cookie_values, time() + (86400 * 30), "/");
} elseif (!isset($_COOKIE["nav_history"])) {
setcookie("nav_history", "Home<br>", time() + (86400 * 30), "/");
}
echo $_COOKIE['nav_history'];
?>
Code for feedback page
<?php
if (isset($_COOKIE['nav_history'])) {
$cookie_values = $_COOKIE["nav_history"];
$cookie_values .= "Feedback<br>";
setcookie("nav_history", $cookie_values, time() + (86400 * 30), "/");
} elseif (!isset($_COOKIE["nav_history"])) {
setcookie("nav_history", "Feedback<br>", time() + (86400 * 30), "/");
}
echo $_COOKIE['nav_history'];
?>
I have this feeling both problem scenarios can be fixed with the same change in code. Sorry if my explaining is not that clear.
<?php
if (isset($_COOKIE['nav_history'])) {
$cookie_values = $_COOKIE["nav_history"];
$cookie_values .= "Feedback<br>";
setcookie("nav_history", $cookie_values, time() + (86400 * 30), "/");
} elseif (!isset($_COOKIE["nav_history"])) {
setcookie("nav_history", "Feedback<br>", time() + (86400 * 30), "/");
}
echo $cookie_values ?? ''; //this line had to be changed.
?>
I have a website that has multiple locations and each location has their own set of information. When a user visits the main corp page, the location information will not display. When the user then goes to a location page I have a cookie being set.
$location_address = get_field('location_address','options');
$location_city = get_field('location_city','options');
$location_state = get_field('location_state','options');
$location_zip_code = get_field('location_zip_code','options');
$location_phone_number = get_field('location_phone_number','options');
// cookie will expire when the browser close
setcookie("locationAddress",$location_address);
setcookie("locationCity",$location_city);
setcookie("locationState",$location_state);
setcookie("locationZipcode",$location_zip_code);
setcookie("locationPhone",$location_phone_number);
The cookie is showing my location information in the firebug console. However, what is happening is when I go to another location, its grabbing both sets of cookies. I need it to replace the cookie previously set with the new one.
This is my code as well to output the cookie, which seems to be breaking as well:
<div class="top_contact">
<p><?php if(!isset($_COOKIE[$location_address])) { echo "" . $location_address . "";} ?>, <?php if(!isset($_COOKIE[$location_city])) { echo "" . $location_city . "";} ?> <?php if(!isset($_COOKIE[$location_state])) { echo "" . $location_state . "";} ?> <?php if(!isset($_COOKIE[$location_zip_code])) { echo "" . $location_zip_code . "";} ?> | <span class="top_phone"><?php if(!isset($_COOKIE[$location_phone_number])) { echo "" . $location_phone_number . "";} ?></span> </p>
</div>
You should set "path" argument in setcookie.
setcookie("locationAddress",$location_address, 0, "/");
setcookie("locationCity",$location_city, 0, "/");
setcookie("locationState",$location_state, 0, "/");
setcookie("locationZipcode",$location_zip_code, 0, "/");
setcookie("locationPhone",$location_phone_number, 0, "/");
If I were you I would probably try and set the path for the cookies.
setcookie($cookie_name, $cookie_value, time() + (86400 * 30), "/"); // 86400 = 1 day
also, I spotted a few commas in your code and I fixed the spaghetti code.
<?php
echo '<div class="top_contact"><p>';
if(!isset($_COOKIE[$location_address]))
{
echo $location_address;
}
if(!isset($_COOKIE[$location_city]))
{
echo $location_city;
}
if(!isset($_COOKIE[$location_state]))
{
echo $location_state;
}
if(!isset($_COOKIE[$location_zip_code]))
{
echo $location_zip_code;
}
echo '<span class="top_phone">';
if(!isset($_COOKIE[$location_phone_number]))
{
echo $location_phone_number;
}
echo '</span> </p> </div>';
?>
I made a card game with PHP but there I'm facing some issue.
if ($_SESSION["bet"] != NULL)
{
echo "Your starting bankroll is: " . $_SESSION["bankroll"] . "<br>";
echo "Your bet is: " . $_SESSION["bet"] . "<br>";
}
I'm getting an input from the user. The problem is, when the game loads at first, and the user enters an input and clicks submit, the game won't work. The condition ($_SESSION["bet"] != NULL) is giving true and bankroll is not defined.
Is there a way I can set this up properly? Is there some PHP method that can initialize the variable once then only works it on session start, then the rest of the code can take care of how that variable gets updated? The bankroll variable gets initialized if the user clicks submit without anything in it right now, so the game still works but it starts improperly.
if ($_SESSION["bet"] == NULL)
{
$_SESSION["bankroll"] = 1000;
}
The bankroll variable gets initialized to 1000 every time user submits a NULL input. I want to change this.
More code... Updating...
session_start();
$_SESSION["bet"] = $_POST["bet"];
echo "<br>";
//print_r($_SESSION);
if ($_SESSION["bet"] != NULL)
{
echo "Your starting bankroll is: " . $_SESSION["bankroll"] . "<br>";
echo "Your bet is: " . $_SESSION["bet"] . "<br>";
}
if ($_SESSION["bet"] == NULL)
{
$_SESSION["bankroll"] = 1000;
}
else if ($_SESSION["bet"] > 1000 || $_SESSION["bet"] < 0)
{
echo " Please enter between 0 and 1000.";
}
else if ($_SESSION["bet"] > $_SESSION["bankroll"])
{
echo "You can't enter more than what you have.";
}
else
{
$deck = array();
for($x = 0; $x < 54; $x++) {
$deck[$x] = $x;
}
shuffle($deck);
//Then more stuff. This one for example...
if(($houseSuits[0] == -100) || ($houseSuits[1] == -100) || ($houseSuits[2] == -100) || ($houseSuits[3] == -100))
{
echo "<br>";
echo '<center> PLAYER WINS! (HOUSE HAS JOKER) </center>';
echo "<br>";
$_SESSION["bankroll"] = $_SESSION["bankroll"] + $_SESSION["bet"]; //THESE NEED TO BE ADDRESSED.
}
I JUST WANT TO FIND A WAY TO INITIALIZE BANKROLL TO 1000 AT START. THE WAY I'M DOING IT IS BY SUBMITTING A NULL VALUE THEN ASSUMING USER NEVER SUBMITS NULL VALUE AGAIN.
I WOULD LIKE BANKROLL TO BE INITIALIZED TO 1000, THEN FOR THE GAME TO TAKE CARE OF HOW BANKROLL GETS UPDATED.
I FOUND A WAY TO DO IT BUT IT'S NOT A PROPER WAY SO THAT'S WHY I'M ASKING FOR HELP.
THANK YOU.
Ok try this.
So if the bankroll is not set, then set it, once it's set it wont get set again because it's set.
Then any conditions after are fine.
session_start();
// Initialise bankroll if not already
if (!isset($_SESSION['bankroll'])) {
$_SESSION['bankroll'] = 1000;
echo "Your starting bankroll is: " . $_SESSION["bankroll"] . "<br>";
}
$_SESSION['bet'] = $_POST['bet'];
if ($_SESSION['bet'] != NULL) {
echo "Your bet is: " . $_SESSION['bet'] . "<br>";
}
if ($_SESSION['bet'] > 1000 || $_SESSION['bet'] < 0) {
echo " Please enter between 0 and 1000.";
} elseif ($_SESSION['bet'] > $_SESSION['bankroll']) {
echo "You can't enter more than what you have.";
} else {
// Your card game stuff
}
Notes: you may have issues with using session as it'll store their bet wherever they are, so issuing a bet sets the session, then navigate elsewhere and come back and their bet will still be as before. Maybe this doesn't matter.
You also might not want to check if the bet is null, more perhaps empty or whatever. Test either way to be sure.
Let’s suppose I‘ve set cookie in PHP, like so:
cookie_name = "USER_PRODUCT".rand(0,9999);
$cookie_value = $name;
setcookie($cookie_name, $cookie_value, time() + (86400 * 30), "/"); // 86400 = 1 day
echo "Successs";
And I display those cookie data like this:
<?php foreach ($_COOKIE as $key=>$val) { ?>
<div class="row col-sm-4 productDiv" data-id="myProduct_<?=$i?>" style="margin: 0px;">
<div class="alert alert-info mydiv"><?=$val?></div>
</div>
<?php } ?>
Now on another page I need to display all cookies whose name starts with USER_PRODUCT. Is it possible in PHP or is there another way?
This is a solution without the explode.
<?php
foreach ($_COOKIE as $key=>$val) {
if (substr($key, 0, 12) == "USER_PRODUCT") {
echo $key . " - " . $val . "<br>";
}
}
Assuming your page 1 code is as follow
$name="topupStackoverflow";
$cookie_name = "USER_PRODUCT".rand(0,9999);
$cookie_value = $name;
setcookie($cookie_name, $cookie_value, time() + (86400 * 30), "/");
echo "Successs";
you can do this for page 2 (or any other application where the user_product can be located at any position)
$stringn="USER_PRODUCT";
foreach($_COOKIE as $cookie => $taste){
if(stristr($cookie,$stringn)!=false){
echo $cookie." = = >>".$taste."<br>";
}
}
while ($row = mysql_fetch_row($result))
{
echo "<tr>";
echo ("<p><td>$row[2]</td><td>$row[0]</td><td>$row[1]</td><td><i>$row[3]</i></td><td><center>[x]</center></td></p>");
echo "</tr>";
$x++;
}
echo "</table>";
}
else
{
echo "*No Accounts*";
}
if (isset($_COOKIE['amountx'])) {
if ($_COOKIE['amountx'] < $x) {
$x = $x - $_COOKIE['amountx'];
echo "<title>'New Logs - ($x)'</title>";
}
else if ($_COOKIE['amountx'] == $x) {
echo "<title>'Logs (0)'</title>";
}
else {
setcookie("amountx", $x, time() + 60 * 60 * 24 * 30);
}
}
else {
setcookie("amountx", $x, time() + 60 * 60 * 24 * 30);
}
The title never updates but the cookie is saved. This was in the while loop but I took it out and it still saves the cookie amount. But I can't get it to display the new title even after refreshing every 5 seconds via meta-refresh. How can I update the title?
Looking at the structure of your code, it appears that you're printing HTML in the <body> tag before you're attempting to echo a different <title> tag.
You can't have a <title> tag anywhere but within the <head> element.
Move your <title> code to take effect within the <head> element, and it should work.
I removed the top <title>Page title</title> at the top of my page now it works properly thanks guys.