close ad
Databridge V2 with MySQLi support IS Now Available!
open ad
View Menu

Technical Support Forums

Free, outstanding support from WebAssist and your colleagues

rating

How to redirect users to previous page using SESSION and/or COOKIES

Thread began 4/27/2012 6:17 pm by danny405167 | Last modified 4/30/2012 9:42 am by Jason Byrnes | 3834 views | 1 replies |

danny405167

How to redirect users to previous page using SESSION and/or COOKIES

Is there a solution recipe for the following:
User logins from main page -> user is redirected to user profile page
if user logins from shop.php page -> users is redirected to previous page

Below is a pieceof code that I found on this forum and works perfect for redirecting users based on their user level. Now I would like to make it redirect to previous page when users do not log in from main page.

<?php
if(!session_id()) session_start();
switch($_SESSION['userLevel']) {
case "1":
header("Location: admin_index.php");
break;
case "2":
header("Location: clients_index.php");
break;
}
?>


The problem that I have is
1) Can the above code have an else if statement to add the third scenario? If so, can you please tell what is wrong with the code below.

<?php
if(!session_id()) session_start();
switch($_SESSION['userLevel']) {
case "1":
header("Location: admin_index.php");
break;
case "2":
header("Location: clients_index.php");
break;
}
else if(!session_id()) session_start();
switch($_SESSION['userLevel']=9) {
case "3":
header("Location: admin_index.php");
}
?>

2) How do I add a SESSION to shop.php page so that Security Assist knows to redirect the user to the previous page and not the control page. I found and added the following code to the shop.php page but no luck. Does Security Assist has a function to add the SESSION to the page or I will have to hand code it allays?

<?php
if (!session_id()) session_start();
if(!isset($_SESSION["userLevel"])) {
$_SESSION["userLevel"] = "9";
}
?>

Sign in to reply to this post

Jason ByrnesWebAssist

1) This code uses the switch case concept. You cannot add an else statement to the switch .. case.


think of it as a switchboard.


the switch statement sets a variable to be used for comparison. the case statement sets an action to be performed if the comparison variables matches the case value.

the code:

php:
switch($_SESSION['userLevel']) { 

    case "1": 
        header("Location: admin_index.php"); 
        break; 
    case "2": 
        header("Location: clients_index.php"); 
        break; 
}



could also be written using if... else, but switch... case is more efficient:

php:
<?php

if($_SESSION['userLevel'] == "1") {
    
header("Location: admin_index.php"); 
} elseif(
$_SESSION['userLevel'] == "2") {
    
header("Location: clients_index.php"); 
}
?>




to add a third scenario to the switch... case code, you just need to add another case statement:

php:
switch($_SESSION['userLevel']) { 

    case "1": 
        header("Location: admin_index.php"); 
        break; 
    case "2": 
        header("Location: clients_index.php"); 
        break; 
    case "3": 
        header("Location: admin_index.php"); 
        break; 
}




2) Redirecting back to the previous page is not done using a session variable. when you access a proteced page, it redirects to the login page with a querystring varaible that contains the path to the protected page named "accesscheck":
users_login.php?accesscheck=%2Fsculptortocms%2Fadmin_cms%2Findex.php%3F

just make sure that the action of the login form on the login page is set to pass any existing querystreing variables by using this code for the action attribute:

php:
 action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?><?php echo (isset($_SERVER["QUERY_STRING"]) && $_SERVER["QUERY_STRING"] != "")?"?".htmlspecialchars($_SERVER["QUERY_STRING"]):""?>"
Sign in to reply to this post

Build websites with a little help from your friends

Your friends over here at WebAssist! These Dreamweaver extensions will assist you in building unlimited, custom websites.

Build websites from already-built web applications

These out-of-the-box solutions provide you proven, tested applications that can be up and running now.  Build a store, a gallery, or a web-based email solution.

Want your website pre-built and hosted?

Close Windowclose

Rate your experience or provide feedback on this page

Account or customer service questions?
Please user our contact form.

Need technical support?
Please visit support to ask a question

Content

rating

Layout

rating

Ease of use

rating

security code refresh image

We do not respond to comments submitted from this page directly, but we do read and analyze any feedback and will use it to help make your experience better in the future.

Close Windowclose

We were unable to retrieve the attached file

Close Windowclose

Attach and remove files

add attachmentAdd attachment
Close Windowclose

Enter the URL you would like to link to in your post

Close Windowclose

This is how you use right click RTF editing

Enable right click RTF editing option allows you to add html markup into your tutorial such as images, bulleted lists, files and more...

-- click to close --

Uploading file...