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
https://forums.phpfreaks.com/topic/18931-simple-question-about-cookies/
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]
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]

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.