Forum Moderators: coopster

Message Too Old, No Replies

session script in php

session, php

         

freshfromseo

7:38 am on Feb 8, 2008 (gmt 0)

10+ Year Member



hello,

im new to web development, can anybody help me with my prob.
i am doing the login in the home page. and i am using session in my login. the prob is it wont go to the location that it supposed to be in. below is my source code, can anyone fix this prob?

<?php
session_start();
include('../dbconnect/connect.php');
require_once('../includes/encryption.class.inc');

$crypt = new encryption_class;

$adj = $crypt->getAdjustment();
$mod = $crypt->getModulus();

$email=$_POST['email'];
$password=$_POST['pword'];

if($_SESSION['logged']){//if still logged in, redirect to home
if(isset($_SESSION['url'])){
header("Location: http://".$_SESSION['url']);
}else{
header("Location: http://".$_SERVER['SERVER_NAME']."/home.php");
}
}

if(isset($_POST['Login']))
{
if(empty($email) ¦¦ empty($password))
{
$msg="Please fill in the field!";
include('../home.php');
exit;
}
elseif(!preg_match("/^([a-z0-9._-](\+[a-z0-9])*)+@[a-z0-9.-]+\.[a-z]{2,6}$/",$email))
{
$msg = "Email add is incorrect!";
include("../home.php");
exit;
}
else
{
$sql="select *from registration where email='$email'";
$data = mysql_query($sql);
while($row=mysql_fetch_array($data))
{
$dbemail=$row['email'];
$dbpassword=$row['password'];
$dbstatus=$row['status'];
$dbusername=$row['username'];
}

//encripts mobitms password
$encrypt_password = $crypt->encrypt($dbusername, $password, 16);
$errors = $crypt->errors;
//end of mobitmsads password encription

if(($dbemail!=$email))
{
$msg = "Invalid email address!";
include('../home.php');
exit;
}
elseif($dbpassword!=$encrypt_password)
{
$msg="Invalid password!";
include('../home.php');
exit;
}
elseif($dbstatus!='Active')
{
$msg="Please activate your account!";
include('../home.php');
exit;
}
else
{
$_SESSION['logged'] = $email;
if(isset($_SESSION['url'])){
header("Location: http://".$_SESSION['url']);
}else{
header("Location: http://".$_SERVER['SERVER_NAME']."/home.php");
}
}
}
}
else
{
include('../home.php');
exit;
}

?>

it supposed to be after i pressed the login button when everything is ok if i inputted the right email add and password, i will be directed to the /my-campaign/index.php. but the result is the reverse of it. instead of going to my-campaign/index.php it is stuck on home.php. And if i inputted the wrong email add it is ok coz the page is on home.php but the prob is it wont display the error message. but if i will get the if($_SESSION['logged']){//if still logged in, redirect to home
if(isset($_SESSION['url'])){
header("Location: http://".$_SESSION['url']);
}else{
header("Location: http://".$_SERVER['SERVER_NAME']."/home.php");
}
}

and also this
else
{
$_SESSION['logged'] = $email;
if(isset($_SESSION['url'])){
header("Location: http://".$_SESSION['url']);
}else{
header("Location: http://".$_SERVER['SERVER_NAME']."/home.php");
}
}

the error messages will display but it doesnt have a session anymore... what should i do? please help me. the action of my home.php is /my-campaign/.....please help me.

[edited by: eelixduppy at 7:49 am (utc) on Feb. 8, 2008]
[edit reason] delinked code [/edit]

menace_sa

12:20 pm on Feb 8, 2008 (gmt 0)

10+ Year Member



Wow thats a lot of code for a login...

I usually use something like this
<?php
session_start();
$user = "dbuser";
$pass = "dbpass";
$host = "localhost";
$database = "db";

global $host, $user, $pass, $database, $username, $password;
$db_bks = mysql_pconnect($host,$user,$pass);
if (!$db_bks) {
echo "Login failed.";
exit;}

mysql_select_db($database);

$act = $_POST["Submit"];

if ($act == "Login")
{
//CHECK FOR USERNAME AND PASSWORD IN ADMIN LIST
$user = $_POST["user"];
$pass = $_POST["pass"];

if (($user <> '') and ($pass <> ''))
{
$getuser = "Select * from admin where username = '$user' and password = '$pass'";
$gotuser = mysql_query($getuser);
echo mysql_error();

$countresult = mysql_num_rows($gotuser);

if ($countresult > 0)
{
//ITS A USER , LET HIM IN AND ALSO SET SESSION VARIABLES AS WELL AS LOGIN TIME
$_SESSION['isuser'] = 'Y';
$id = mysql_result($gotuser,0,'id');
$now = date('Y-m-d H:i:s');

$updatelogin = "Update admin set last_login = '$now'";
$doupdate = mysql_query($updatelogin);
echo mysql_error();

header("Location: index_admin.php");

}
else
{
echo "<script language='Javascript'>alert ('Incorrect Username or Password , please try again')</script>";
}
}
}

?>

freshfromseo

1:44 am on Feb 9, 2008 (gmt 0)

10+ Year Member



yes it's really a long code for login...hehehe.... tnx menace_sa for your time and replying to my prob. actually the prob in my code is in my condition. i should not put else after the condition elseif($dbstatus!='Active')... but anyway...tnx menace_sa