Jump to content


Photo

IF, setcookie help


  • Please log in to reply
8 replies to this topic

#1 kevdotbadger

kevdotbadger
  • Members
  • PipPip
  • Member
  • 18 posts

Posted 24 October 2006 - 06:29 PM

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.

<?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";
}

?>

now can someone explain why it just ignores setcookie

#2 marcus

marcus
  • Members
  • PipPipPip
  • Advanced Member
  • 1,842 posts
  • LocationRochester, NY

Posted 24 October 2006 - 06:41 PM

probably best if you used a working login script

you can use mine if you want

<?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";
  }

?>

then on pages if it requires authorization

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


#3 trq

trq
  • Staff Alumni
  • Advanced Member
  • 31,041 posts

Posted 24 October 2006 - 06:44 PM

now can someone explain why it just ignores setcookie


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.

#4 marcus

marcus
  • Members
  • PipPipPip
  • Advanced Member
  • 1,842 posts
  • LocationRochester, NY

Posted 24 October 2006 - 06:46 PM

or if you arent using mysql for this

<?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";
};
?>

then for pages that require login

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


#5 kevdotbadger

kevdotbadger
  • Members
  • PipPip
  • Member
  • 18 posts

Posted 24 October 2006 - 06:49 PM

now can someone explain why it just ignores setcookie


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.


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.

#6 trq

trq
  • Staff Alumni
  • Advanced Member
  • 31,041 posts

Posted 24 October 2006 - 06:51 PM

Does the message login correct or login incorrect ever display?

#7 marcus

marcus
  • Members
  • PipPipPip
  • Advanced Member
  • 1,842 posts
  • LocationRochester, NY

Posted 24 October 2006 - 06:52 PM

you should define the cookie value for the other one as well, make it false.



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

#8 kevdotbadger

kevdotbadger
  • Members
  • PipPip
  • Member
  • 18 posts

Posted 24 October 2006 - 06:58 PM

or if you arent using mysql for this

<?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";
};
?>

then for pages that require login

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


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!

#9 marcus

marcus
  • Members
  • PipPipPip
  • Advanced Member
  • 1,842 posts
  • LocationRochester, NY

Posted 24 October 2006 - 07:04 PM

no problem




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users