2015-07-28 106 views
-2

請參見下面的代碼:使用會話變量登錄後,在其他PHP文件

的login.php

<?php 
session_start(); // Starting Session 
$error=''; // Variable To Store Error Message 
if (isset($_POST['submit'])) { 
if (empty($_POST['username']) || empty($_POST['password'])) { 
$error = "Username or Password is invalid"; 
} 
else 
{ 
// Define $username and $password 
$username=$_POST['username']; 
$password=$_POST['password']; 
// Establishing Connection with Server by passing server_name, user_id and password as a parameter 
$connection = mysql_connect("localhost", "root", ""); 
// To protect MySQL injection for Security purpose 
$username = stripslashes($username); 
$password = stripslashes($password); 
$username = mysql_real_escape_string($username); 
$password = mysql_real_escape_string($password); 
// Selecting Database 
$db = mysql_select_db("company", $connection); 
// SQL query to fetch information of registerd users and finds user match. 
$query = mysql_query("select * from login where password='$password' AND username='$username'", $connection); 
$rows = mysql_num_rows($query); 
if ($rows == 1) { 
$_SESSION['login_user']=$username; // Initializing Session 
header("location: profile.php"); // Redirecting To Other Page 
} else { 
$error = "Username or Password is invalid"; 
} 
mysql_close($connection); // Closing Connection 
} 
} 
?> 

profile.php

<?php 
// Establishing Connection with Server by passing server_name, user_id and password as a parameter 
$connection = mysql_connect("localhost", "root", ""); 
// Selecting Database 
$db = mysql_select_db("company", $connection); 
session_start();// Starting Session 
// Storing Session 
$user_check=$_SESSION['login_user']; 
// SQL Query To Fetch Complete Information Of User 
$ses_sql=mysql_query("select username from login where username='$user_check'", $connection); 
$row = mysql_fetch_assoc($ses_sql); 
$login_session =$row['username']; 
if(!isset($login_session)){ 
mysql_close($connection); // Closing Connection 
header('Location: index.php'); // Redirecting To Home Page 
} 
?> 
<html> ............... </html> 

我們應該使用下面顯示的每個php文件中下面的代碼部分?

<?php 
// Establishing Connection with Server by passing server_name, user_id and password as a parameter 
$connection = mysql_connect("localhost", "root", ""); 
// Selecting Database 
$db = mysql_select_db("company", $connection); 
session_start();// Starting Session 
// Storing Session 
$user_check=$_SESSION['login_user']; 
// SQL Query To Fetch Complete Information Of User 
$ses_sql=mysql_query("select username from login where username='$user_check'", $connection); 
$row = mysql_fetch_assoc($ses_sql); 
........... ?> 

我學會創建使用此site。但當我看到profile.php它使用的代碼部分上面的的login.phpprofile.php:檢查連接和數據庫然後使用$user_check=$_SESSION['login_user'];,然後用下面的代碼:

$ses_sql=mysql_query("select username from login where username='$user_check'", $connection); 
$row = mysql_fetch_assoc($ses_sql); 

如果我有另一個php文件,如下面的代碼使用支付網關:

send.php

<?php 
include_once("sender.php"); 
$url = 'http://example.com/payment/gateway-send'; 
$api = ' Your-API '; 
$amount = 1000; 
$redirect = 'REDIRECT-PAGE'; 
$result = send($url,$api,$amount,$redirect); 
if($result > 0 && is_numeric($result)){ 
$go = "http://example.com/payment/gateway-$result"; 
header("Location: $go"); 
} 
?> 

我應該在send.php文件的開頭,也爲其他php文件添加以下代碼?

<?php 
// Establishing Connection with Server by passing server_name, user_id and password as a parameter 
$connection = mysql_connect("localhost", "root", ""); 
// Selecting Database 
$db = mysql_select_db("company", $connection); 
session_start();// Starting Session 
// Storing Session 
$user_check=$_SESSION['login_user']; 
// SQL Query To Fetch Complete Information Of User 
$ses_sql=mysql_query("select username from login where username='$user_check'", $connection); 
$row = mysql_fetch_assoc($ses_sql); 
.................?> 
+2

你的問題是什麼? –

+0

@Prashant Srivastav:我想知道如何在每個php文件中使用session?我應該添加我的文章中的最新代碼在其他我的PHP文件? – java

+0

您需要在每個要使用'$ _SESSION'數組的頁面中使用'session_start()' – Umair

回答

0

我建議你創建一個連接文件說connection.php與代碼 -

<?php 
    // Establishing Connection with Server by passing server_name, user_id and password as a parameter 
    $connection = mysql_connect("localhost", "root", ""); 
    // Selecting Database 
    $db = mysql_select_db("company", $connection); 
?> 

而另一個文件security.php設置會話變量。

session_start();// Starting Session 
// Storing Session 
$user_check = $_SESSION['login_user']; 
// SQL Query To Fetch Complete Information Of User 
$ses_sql = mysql_query("select username from login where username = '$user_check'", $connection); 
$row = mysql_fetch_assoc($ses_sql); 
.................?> 

並在所有php文件的開頭包含這兩個文件。

在send.php我無法找到任何數據庫相關的操作或會話檢查。所以不需要在send.php中包含這兩個文件。