2015-11-04 164 views
0

我想知道如何在用户点击添加到购物车按钮时登录到php
例如用户点击添加到购物车按钮,如果该用户没有登录,那么用户将被重定向到登录页面,登录后用户将被重定向到请求页面 的购物车页面,所以我想知道如何为此编码。如何在用户登录后将用户重定向到特定链接php


if(!empty($_SESSION['username'])) 
{ 
     $url=' .php';//here i want dynamic link which is requested page link when user tries to access 
} 
else 
{ 
     $url='login.php'; 
} 
+1

确定。那么代码在哪里? –

+0

其实我没有得到任何关于如何编写代码的想法,这就是为什么我问这个问题。 – Dipesh

+0

可能这是可能使用cookie,但不知道如何 – Dipesh

回答

0

你要通过这些变量,供大家参考知道从哪个页面你都来了。

您必须为此使用header()

session_start();// beginning of your file. 
if(user is logged in) { 
    start checking for conditions here. 
    if($_GET['from'] == "checkout" 
      $file="checkout.php"; 
    else 
      $file="index.php"; 

    header("Location: $file");// file to which you want to redirect after login. 
} else { 
    // redirect to login page. 
} 

您可以从您来到此页面的位置传递,然后检查用户是否已登录,然后决定重定向的位置。

+0

感谢您的回复,但我知道这个基本的代码。我想你没有得到我的问题 – Dipesh

+0

@Dipesh我编辑过。 –

+0

我已更新我的问题。为更好的理解我想要什么 – Dipesh

0

对Google来说,这可能会比您更快地将它输入到StackOverflow中,并等待监控响应,但是谁会说。也许您的浏览器已经在此页面打开,并且您使用的是旧拨号调制解调器,因此加载新的非缓存内容非常缓慢。

How to make a redirect in PHP?

1

首先检查用户登录或不

if(isset($_SESSION['username'])){ 
$url='cart.php'; 
}else{ 
$url='login.php'; 
} 
<a href="<?php echo $url; ?>">add to cart</a> 
+0

我已更新我的问题。为更好的理解我想要什么 – Dipesh

+0

你可以请尝试我的答案:) @Dipesh –

+0

好的谢谢。得到它了。 – Dipesh

0

登录形式

<form action="loginchk.php" method="post"> 
     <input type="text" name="unm" id="unm" /> 
     <input type="password" name="pwd" id="pwd" /> 
     <input type="hidden" name="redirect_url" value="<?php echo basename($_SERVER['PHP_SELF']); ?>" /> 
     <input type="submit" value="Login" /> 
</form> 

loginchk.php

<?php 
    if(!empty($_POST)){ 
     extract($_POST); 

     //sql query 

     if(//true condition){ 
       header("location:".$redirect_url); 
     } else{ 
       header("location:login.php"); 
     } 
    } 
?> 
相关问题