Jump to content

IF, setcookie help


kevdotbadger

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
Link to comment
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]
Link to comment
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]
Link to comment
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.
Link to comment
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!
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.