Gruzin Posted August 28, 2006 Share Posted August 28, 2006 hi guys,Here is my little problem: if the cookies is set redirect user to the page and if not redirect to another page. Here is the code, please tell me what I'am not doing correctly, thanks in advance.P.S I use this to set cookies, but it's included in other page:[code]<?phpsetcookie("login", time()+60);?>[/code][code]<?phpif(isset($HTTP_COOKIE_VARS["login"])){header ('Location: http://www.mysite.net/admin.php'); // if cookies are set redirect user to the admin panel}else{header ('Location: http://www.mysite.net/log.php');}?>[/code] Link to comment https://forums.phpfreaks.com/topic/18931-simple-question-about-cookies/ Share on other sites More sharing options...
Gruzin Posted August 28, 2006 Author Share Posted August 28, 2006 please help, I really need this... Link to comment https://forums.phpfreaks.com/topic/18931-simple-question-about-cookies/#findComment-81769 Share on other sites More sharing options...
newb Posted August 28, 2006 Share Posted August 28, 2006 try this[code]<?phpif (isSet ($HTTP_COOKIE_VARS["login"]) ) { echo '<meta http-equiv="Refresh" Content="0; URL=http://www.mysite.net/admin.php">'; // if cookies are set redirect user to the admin panel} else {echo '<meta http-equiv="Refresh" Content="0; URL=http://www.mysite.net/log.php">';}?>[/code] Link to comment https://forums.phpfreaks.com/topic/18931-simple-question-about-cookies/#findComment-81770 Share on other sites More sharing options...
Gruzin Posted August 28, 2006 Author Share Posted August 28, 2006 sorry but it doesn't work for me... Any other ideas? thanks for your time Link to comment https://forums.phpfreaks.com/topic/18931-simple-question-about-cookies/#findComment-81775 Share on other sites More sharing options...
newb Posted August 28, 2006 Share Posted August 28, 2006 why not use sessions instead Link to comment https://forums.phpfreaks.com/topic/18931-simple-question-about-cookies/#findComment-81776 Share on other sites More sharing options...
Gruzin Posted August 28, 2006 Author Share Posted August 28, 2006 Is that the solution? I don't know, I'am just a biginner :)Where can I find good tutorial of sessions? And can I check if cookie is set using sessions? thank u very much for your help. Link to comment https://forums.phpfreaks.com/topic/18931-simple-question-about-cookies/#findComment-81780 Share on other sites More sharing options...
newb Posted August 28, 2006 Share Posted August 28, 2006 yes you can. sessions are much easier to use then cookies. here's my code, it should work for u:[code]<?phpsession_start(); if(isset($_SESSION['username'])) { $ses_user = $_SESSION['username']; echo "You are already logged in, $ses_user. Redirecting to User CP..."; echo "<meta http-equiv='refresh' content='3;url=http://mysite.com/index.php?name=usercp'>"; } else { // Begin Login Code if(isset($_POST['login'])) { $error = ''; $username = $_POST['username']; $password = $_POST['password']; if(!isset($username) || !isset($password)) { $error .= 'A required field was left blank.<br />'; } $password = md5($password); if(get_magic_quotes_gpc()) { $username = $username; } else { $username = addslashes($username); } $result = $libmysql->query("SELECT * FROM $table_users WHERE username='$username' AND password='$password'"); $valid_login = mysql_num_rows($result); if($valid_login == 0) { $error .= 'The supplied username and/or password was incorrect.<br />'; } if($error == '') { $data = mysql_fetch_array($result); $_SESSION['username'] = $data['username']; echo '<meta http-equiv="Refresh" Content="0; URL=http://mysite.com/index.php?name=usercp">'; die(); } else { echo 'The following errors were returned:<br />'.$error.'<br />'; } }?>[/code]then for the usercp:[code]<?php if(isset($_SESSION['username'])) { echo "Welcome to the userCP $ses_user. You are logged in"; } else { echo "You aren't logged in.<br /><br />Please <a href='mysite.com/index.php?name=signup'>register</a> or <a href='mysite.com/index.php?name=usercp'>log in.</a>"; }?>[/code] Link to comment https://forums.phpfreaks.com/topic/18931-simple-question-about-cookies/#findComment-81784 Share on other sites More sharing options...
Gruzin Posted August 28, 2006 Author Share Posted August 28, 2006 ok thank u very much! I'll try that ;) Link to comment https://forums.phpfreaks.com/topic/18931-simple-question-about-cookies/#findComment-81785 Share on other sites More sharing options...
newb Posted August 28, 2006 Share Posted August 28, 2006 np, dont forget to add a html form, thats just the php code. Link to comment https://forums.phpfreaks.com/topic/18931-simple-question-about-cookies/#findComment-81787 Share on other sites More sharing options...
Gruzin Posted August 28, 2006 Author Share Posted August 28, 2006 ok sure :) Link to comment https://forums.phpfreaks.com/topic/18931-simple-question-about-cookies/#findComment-81789 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.