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.

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.