我根本不是一个程序员,所以我需要一些帮助来解决这个问题:
我需要将Cookie和会话设置为一个像这样工作的referal系统: www.mywebsite.com/?id=affiliate
我的一个问题是当一个被推荐的客户来到我的网站时没有 ../?id = affiliate 的链接,因为我需要检查是否有一些cookie被注册,如果不要使用默认会员设置全新的cookie。
我已经写了这个代码,但我确信它不正确。 请你好好看看并给出一些建议。我很感激。
代码:
$id = $_POST("id");
if ($_POST("id") = null) {
if( isset( $_COOKIE['pro'] ) )
{
$id = $_COOKIE['pro'];
}else{
$id = "defaultaffiliate"
}
}
session_start();
$rs = mysql_query("select * from users where Username='$id'");
$arr=mysql_fetch_array($rs);
if(mysql_num_rows($rs) > 0){
$uid=$arr['id'];
$_SESSION["name"]=$arr['Name'];
setcookie("pro", $uid, time()+30 * 24 * 3600);
}else{
$uid='claudioxerez';
$_SESSION["name"]='Claudio Xerez';
setcookie("pro", $uid, time()+30 * 24 * 3600);
}
答案 0 :(得分:0)
对于Set Session和Cookie:
Config.php:
$SERVER="localhost";
$DB_NAME="********";
$USERNAME="*******";
$PASSWORD="*******";
$CONN=mysql_connect($SERVER,$USERNAME,$PASSWORD);
mysql_query("set names UTF8",$CONN);
if(!$CONN)
{
die('ERROR IN CONNECT TO DATABASE!'.mysqli_connect_error());
mysql_close();
}
else
{
mysql_select_db($DB_NAME,$CONN);
}
Login.php:
<form enctype="multipart/form-data" method="post" action="Config/Checklogin.php">
<input name="txtUsername" type="text" id="txtUsername" autocomplete="off" />
<input name="txtPassword" type="password" id="txtPassword" />
<p>Remember ME .</p><input name="chkRemember" id="chkRemember" type="checkbox" /><
<input type="submit" value="Submit" />
</form>
Config / Checklogin.php:
session_start();
include("Config.php");
if(!empty($_POST["txtUsername"])&& !empty($_POST["txtPassword"]))
{
$User=$_POST["txtUsername"];
$PASSWORD=$_POST["txtPassword"];
$Remember=$_POST["chkRemember"];
setSession($User,$PASSWORD,$Remember);
}
if( (ctype_alnum($User)) )
{
setSession($User,$PASSWORD,$Remember); // --> setSession is a function
}
else
{
header("Location:../Login.php");
}
//------------------------ FUNCTION :
function setSession($YOURUSERNAME,$YOURPASSWORD,$Remember)
{
/* ========================================================= */
$sel="SELECT * FROM TBL_USERS WHERE USERNAME='".SAFE($YOURUSERNAME)."'".
"AND PASSWORD= '".SAFE($YOURPASSWORD)."'";
//=================================================
$Result=QUERY($sel);
$Row=mysql_num_rows($Result);
if($Row==1)
{
if($Remember=='on')
{
setcookie("user", $YOURUSERNAME, time()+3600,"/"); // for an houre
header("location:../index.php");
}
else if($Remember==NULL)
{
$_SESSION["user"]=$YOURUSERNAME;
header("location:../index.php");
}
}
else
{
header("location:../login.php");
}