Jump to content

Simple question about cookies


Gruzin

Recommended Posts

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

[code]<?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');
}
?>[/code]
Link to comment
Share on other sites

try this

[code]
<?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">';
}
?>
[/code]
Link to comment
Share on other sites

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

[code]
<?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 />';
    }
}
?>
[/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
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.