kevdotbadger Posted October 24, 2006 Share Posted October 24, 2006 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 https://forums.phpfreaks.com/topic/24958-if-setcookie-help/ Share on other sites More sharing options...
marcus Posted October 24, 2006 Share Posted October 24, 2006 probably best if you used a working login scriptyou 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]<?phpif($_COOKIE[login] == yes){echo "stuff they can see";}else {echo "bad auth";};?>[/code] Link to comment https://forums.phpfreaks.com/topic/24958-if-setcookie-help/#findComment-113757 Share on other sites More sharing options...
trq Posted October 24, 2006 Share Posted October 24, 2006 [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. Link to comment https://forums.phpfreaks.com/topic/24958-if-setcookie-help/#findComment-113758 Share on other sites More sharing options...
marcus Posted October 24, 2006 Share Posted October 24, 2006 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]<?phpif($_COOKIE[login] == yes){echo "stuff you want them to see";}else{echo "not logged in or bad auth";};?>[/code] Link to comment https://forums.phpfreaks.com/topic/24958-if-setcookie-help/#findComment-113760 Share on other sites More sharing options...
kevdotbadger Posted October 24, 2006 Author Share Posted October 24, 2006 [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 https://forums.phpfreaks.com/topic/24958-if-setcookie-help/#findComment-113761 Share on other sites More sharing options...
trq Posted October 24, 2006 Share Posted October 24, 2006 Does the message [i]login correct[/i] or [i]login incorrect[/i] ever display? Link to comment https://forums.phpfreaks.com/topic/24958-if-setcookie-help/#findComment-113765 Share on other sites More sharing options...
marcus Posted October 24, 2006 Share Posted October 24, 2006 you should define the cookie value for the other one as well, make it false. setcookie("login", "false", time()-3600); Link to comment https://forums.phpfreaks.com/topic/24958-if-setcookie-help/#findComment-113766 Share on other sites More sharing options...
kevdotbadger Posted October 24, 2006 Author Share Posted October 24, 2006 [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]<?phpif($_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 https://forums.phpfreaks.com/topic/24958-if-setcookie-help/#findComment-113770 Share on other sites More sharing options...
marcus Posted October 24, 2006 Share Posted October 24, 2006 no problem Link to comment https://forums.phpfreaks.com/topic/24958-if-setcookie-help/#findComment-113771 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.