Jump to content


Photo

Simple question about cookies


  • Please log in to reply
9 replies to this topic

#1 Gruzin

Gruzin
  • Members
  • PipPipPip
  • Advanced Member
  • 448 posts
  • LocationGeorgia

Posted 28 August 2006 - 07:00 PM

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:
<?php
setcookie("login", time()+60);
?>

<?php
if(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');
}
?>

I don't need your script, I'll try to write it myself

#2 Gruzin

Gruzin
  • Members
  • PipPipPip
  • Advanced Member
  • 448 posts
  • LocationGeorgia

Posted 28 August 2006 - 07:19 PM

please help, I really need this...
I don't need your script, I'll try to write it myself

#3 newb

newb
  • Members
  • PipPipPip
  • Advanced Member
  • 454 posts

Posted 28 August 2006 - 07:22 PM

try this

<?php
if (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">';
}
?>


#4 Gruzin

Gruzin
  • Members
  • PipPipPip
  • Advanced Member
  • 448 posts
  • LocationGeorgia

Posted 28 August 2006 - 07:26 PM

sorry but it doesn't work for me... Any other ideas? thanks for your time
I don't need your script, I'll try to write it myself

#5 newb

newb
  • Members
  • PipPipPip
  • Advanced Member
  • 454 posts

Posted 28 August 2006 - 07:27 PM

why not use sessions instead

#6 Gruzin

Gruzin
  • Members
  • PipPipPip
  • Advanced Member
  • 448 posts
  • LocationGeorgia

Posted 28 August 2006 - 07:30 PM

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.
I don't need your script, I'll try to write it myself

#7 newb

newb
  • Members
  • PipPipPip
  • Advanced Member
  • 454 posts

Posted 28 August 2006 - 07:37 PM

yes you can. sessions are much easier to use then cookies. here's my code, it should work for u:

<?php
session_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 />';
   		   }
		}
?>

then for the usercp:
<?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>";
    }
?>



#8 Gruzin

Gruzin
  • Members
  • PipPipPip
  • Advanced Member
  • 448 posts
  • LocationGeorgia

Posted 28 August 2006 - 07:39 PM

ok thank u very much! I'll try that ;)
I don't need your script, I'll try to write it myself

#9 newb

newb
  • Members
  • PipPipPip
  • Advanced Member
  • 454 posts

Posted 28 August 2006 - 07:42 PM

np, dont forget to add a html form, thats just the php code.

#10 Gruzin

Gruzin
  • Members
  • PipPipPip
  • Advanced Member
  • 448 posts
  • LocationGeorgia

Posted 28 August 2006 - 07:44 PM

ok sure :)
I don't need your script, I'll try to write it myself




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users