Jump to content

Archived

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

kevdotbadger

IF, setcookie help

Recommended Posts

Probably nothing but its bugging me how its not working!

basically a username and password has been send from a form. the values have been catched and wrote into a varialbe. These are matched against 2 pre-defined varialbes. If they match i want to set a cookie, if they dont i want to delete the cookie.

heres my code.

[code=php:0]<?php

$username = "user";
$password = md5("pass");
$try_username = $_POST['username'];
$try_password = md5($_POST['password']);


if(($username == $try_username) && ($password == $try_password)){
setcookie("login", "true", time()+3600);
echo "login correct";
}else{
setcookie("login", "", time()-3600);
        echo "login incorrect";
}

?>[/code]

now can someone explain why it just ignores setcookie

Share this post


Link to post
Share on other sites
probably best if you used a working login script

you can use mine if you want

[code]
<?php
  $connection = mysql_connect(host, dbuser, dbpass;
  $db = mysql_select_db(dbname, $connection);
  $sql = "SELECT * FROM dbtable
          WHERE username='$_POST[username]'
          AND password='$_POST[password]'";

  $result = mysql_query($sql);
  $num = mysql_num_rows($result);
  if ($num > 0) {                                // ### USER AND PASSWORD ARE CORRECT
    $id = mysql_fetch_assoc($result);

    setcookie("login", "yes", time()+3600);

  echo "login correct"


  }else{                                          // ### USER OR PASSWORD IS INCORRECT
    echo "wrong";
  }

?>
[/code]

then on pages if it requires authorization

[code]
<?php
if($_COOKIE[login] == yes){
echo "stuff they can see";
}else {
echo "bad auth";
};
?>
[/code]

Share this post


Link to post
Share on other sites
[quote]now can someone explain why it just ignores setcookie[/quote]

Can you explain what it is that makes you think it is ignoring setcookie. Its in both cases of the expression, so one of them MUST be firing.

Share this post


Link to post
Share on other sites
or if you arent using mysql for this

[code]
<?php
$pass = thepassyouwant;
$user = theuseryouwant;
$password = $_POST[password];
$username= $_POST[username];

if($password == $pass && $username == $user){
echo "login right";
setcookie("login","yes",time()+3600);
}else{
echo "wrong login";
};
?>
[/code]

then for pages that require login

[code]
<?php
if($_COOKIE[login] == yes){
echo "stuff you want them to see";
}else{
echo "not logged in or bad auth";
};
?>
[/code]

Share this post


Link to post
Share on other sites
[quote author=thorpe link=topic=112564.msg456926#msg456926 date=1161715466]
[quote]now can someone explain why it just ignores setcookie[/quote]

Can you explain what it is that makes you think it is ignoring setcookie. Its in both cases of the expression, so one of them MUST be firing.
[/quote]

well i created another page which echos the value of the cookie. both times (after loggin in incorrectly and logging in correctly) they both say that the cookie value is nothing.

Share this post


Link to post
Share on other sites
Does the message [i]login correct[/i] or [i]login incorrect[/i] ever display?

Share this post


Link to post
Share on other sites
you should define the cookie value for the other one as well, make it false.



setcookie("login", "false", time()-3600);

Share this post


Link to post
Share on other sites
[quote author=mgallforever link=topic=112564.msg456928#msg456928 date=1161715562]
or if you arent using mysql for this

[code]
<?php
$pass = thepassyouwant;
$user = theuseryouwant;
$password = $_POST[password];
$username= $_POST[username];

if($password == $pass && $username == $user){
echo "login right";
setcookie("login","yes",time()+3600);
}else{
echo "wrong login";
};
?>
[/code]

then for pages that require login

[code]
<?php
if($_COOKIE[login] == yes){
echo "stuff you want them to see";
}else{
echo "not logged in or bad auth";
};
?>
[/code]
[/quote]

after reading through this (which is basically exactly the same i sussed it!

i forgot the ; after the end of the loop!

thanks! solved ALL my problems!

@mgallforever: yeah i think ill set it now!

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.