Hi

I am using MSSQL ODBC Connetion for my project in PHP. I have created login form with PHP and MSSQL with ODBC Function. Can any one please help how to use SESSION to carry on to next page without get function.

Dani AI

Generated

Good catch moving the session start earlier, . One more tweak to make ’s fix robust: avoid the @ error-suppression operator. If output sneaks out before the session starts (BOM, stray whitespace, echo in an include), you want PHP to tell you. Also, after any Location header, immediately call exit; to stop further output.

If you want this login to be safe and reliable, switch to prepared statements and hashed passwords. With ODBC you can still use ? placeholders and fully qualified objects like [JDE_DEVELOPMENT].[TESTDTA]. Here is a compact pattern you can adapt (note the different session keys to avoid clashing with your current code):

// Hardened login flow (ODBC + sessions)
session_name('appsid');
session_set_cookie_params(['lifetime'=>0,'path'=>'/','secure'=>!empty($_SERVER['HTTPS']),'httponly'=>true,'samesite'=>'Lax']);
session_start();

list($u, $p) = [trim($_POST['username'] ?? ''), $_POST['password'] ?? ''];

$stmt = odbc_prepare($connection, "SELECT ID, PWD_HASH FROM [JDE_DEVELOPMENT].[TESTDTA].[FQ64010] WHERE SWYQ64USRNM = ?");
odbc_execute($stmt, [$u]);

if ($row = odbc_fetch_array($stmt)) {
    $hash = $row['PWD_HASH'] ?? '';
    if (password_verify($p, $hash)) {
        session_regenerate_id(true);
        $_SESSION['uid'] = (int)$row['ID'];
        $_SESSION['uname'] = $u;
        header('Location: loggedon.php'); exit;
    }
}
http_response_code(401);  // generic failure

A quick checklist that solves most “session not carried” issues:

  • Start the session before any output; remove BOM and stray whitespace in all included files.
  • Do not suppress errors with @; enable error_reporting in dev.
  • After redirects: header('Location: ...'); exit;
  • Keep protocol and host consistent (http vs https, www vs bare domain) so the cookie matches.
  • Enable stricter cookies: httponly, secure (on HTTPS), and a SameSite value.
  • Prefer checking a stable key like $_SESSION['uid'] over a flag like LoggedIn.
  • Long term, migrate away from plain-text passwords; store password_hash() output and verify with password_verify().

Recommended Answers

All 4 Replies

Hi,

To carry session from on page to another, Please do the following.
a) Open session before you can do anything on the page. In other words your first line of the page is <?php @session_start(); ?>
b) add values / variable to session i.e. $_SESSION['username'] = 'ajay';
$_SESSION['fburl'] = 'http://www.facebook.com/Amilextech';
etc.

c) When ever you need the session value, put <?php @session_start(); ?> as a first line of your page.

d) Get session values like $fb_link = $_SESSION['fburl']; echo $fb_link ;

Please check and let me know.

Thanks,
Ajay

Hi Ajay

Thanks for your post. But i am unable to get the session after user logged into the page. can any one please help me to solve it

conn.php

<?php
$virtual_dsn = 'DRIVER={SQL Server};SERVER=CESRVR03;DATABASE=JDE_DEVELOPMENT';
$connection = odbc_connect($virtual_dsn,'ss','ss') or die('ODBC Error:: '.odbc_error().' :: '.odbc_errormsg().' :: '.$virtual_dsn);
$database = 'JDE_DEVELOPMENT.TESTDTA';
?>

Login.php

<form name="form1" method="post" action="loginaction.php">
<td>
<table width="100%" border="0" cellpadding="3" cellspacing="1" bgcolor="#FFFFFF">
<tr>
<td colspan="3"><strong>Login </strong></td>
</tr>
<tr>
<td width="78">Username</td>
<td width="6">:</td>
<td width="294"><input name="username" type="text" id="username"></td>
</tr>
<tr>
<td>Password</td>
<td>:</td>
<td><input name="password" type="text" id="password"></td>
</tr>
<tr>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td><input type="submit" name="Submit" value="Login"></td>
</tr>
</table>
</td>
</form>

loginaction.php

<?php 
require_once('conn.php'); 

session_start(); 

// Get the data collected from the user 

$username=$_POST['username'];
$password=$_POST['password'];


$username = stripslashes($username);
$password = stripslashes($password);

function isLoggedIn()
    {
        if($_SESSION['LoggedIn'])
        {
            return true;
        }
        else return false;
    }

$sql="SELECT * FROM $database.FQ64010 WHERE SWYQ64USRNM  ='$username' AND SWYQ64PWD='$password'";
// prepare and execute in 1 statement
$result=odbc_exec($connection,$sql) 
or die ("result error ".odbc_error().'-'.odbc_errormsg());

// if no result: no rows read
if (!odbc_fetch_row($result))
die("Wrong Username or Password"); 

// else: all is okay
else { 
session_regenerate_id();
$_SESSION['LoggedIn'] = true;
$_SESSION['username']=$username;
header("location:loggedon.php");
}

function logout()
    {
        unset($_SESSION['LoggedIn']);
        unset($_SESSION['username']);
        session_destroy();
        header('location: index.php');
    }


odbc_close($connection);
?> 

loggedon.php

<?php
include('conn.php');
session_start(); 
if (!isset($_SESSION['username'])) {
        header('Location: login.php');
}
?>

Hi,

I check your code. Please do the following things
1) Make first linke is @session_start(). (before any include or require statement)
2) in loggedon.php comment line header('Location: login.php');
3) At the end of the code (after if statement) print session values i.e
print_r($_SESSION);

let me know what session values did you get.

Thanks,
Ajay

Hi Ajay

Thanks for your suggestions.

Make first linke is @session_start(). (before any include or require statement)

Its working fine

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.