chriso20 Posted December 20, 2006 Share Posted December 20, 2006 Hey there everyone!Just getting into the magical world of PHP! and by christ is it magical :DI'm currently working on a simple shopping cart system.What i want to do is set a variable that gets echo'd on the "log in" page so it says things like "you need to be logged in to view that page".I already use this same variable to do things like "that password doesn't match that username" things like that.Basically it checks if there's a session called "user" set and if there isnt then it redirects you to "login.php" there's no $_POST 'ing going on so im guessing that's where it fails?Any ideas?Thanks in advance,Chris. Link to comment https://forums.phpfreaks.com/topic/31359-solved-sending-variables-across-pages/ Share on other sites More sharing options...
Orio Posted December 20, 2006 Share Posted December 20, 2006 You can pass variables with the URL (Example- login.php?err=1).Then the login page checks if $_GET['err'] is set, and if it is, echo the error needed. You can have $_GET['err'] set to 1 if password is incorrect, you can set it to 2 if there was a problem with the database etc'.[code]<?phpif(isset($_GET['err'])){ switch($_GET['err']) { case 1: echo "User/Pass dont match"; break; case 2: echo "An error occured, please retry!"; break; //etc' }}?>[/code]On the redirect, you simply add the variable to the URL:[code]header("Location: login.php?err=<error-id-goes-here>");[/code]Orio. Link to comment https://forums.phpfreaks.com/topic/31359-solved-sending-variables-across-pages/#findComment-145092 Share on other sites More sharing options...
chriso20 Posted December 20, 2006 Author Share Posted December 20, 2006 yeah i did know about this, i was just hoping there was a transparent way of doing it - as if i had POST'd some information. (Sorry still working on the terminology!)Another option is a cookie but it all starts to get very long winded for what is simply a "you need to be logged in" message.Any more ideas? can i post without a button and a form? Link to comment https://forums.phpfreaks.com/topic/31359-solved-sending-variables-across-pages/#findComment-145094 Share on other sites More sharing options...
Orio Posted December 20, 2006 Share Posted December 20, 2006 You can use a session variable too.The page the processes login php:[code]<?php$_SESSION['error'] = "User/Pass combination not found";header("Location: login.php");?>[/code]login.php[code]<?phpif(isset($_SESSION['error'])){ echo $_SESSION['error']; unset($_SESSION['error'];}?>[/code]Orio. Link to comment https://forums.phpfreaks.com/topic/31359-solved-sending-variables-across-pages/#findComment-145099 Share on other sites More sharing options...
chriso20 Posted December 20, 2006 Author Share Posted December 20, 2006 that's more like it!Do i have to do the session_start(); thing?cheers, sorry im new :) Link to comment https://forums.phpfreaks.com/topic/31359-solved-sending-variables-across-pages/#findComment-145106 Share on other sites More sharing options...
Orio Posted December 20, 2006 Share Posted December 20, 2006 Yes, of course :)Every time you use a session variable in any sort of way, you need to use session_start().Orio. Link to comment https://forums.phpfreaks.com/topic/31359-solved-sending-variables-across-pages/#findComment-145113 Share on other sites More sharing options...
chriso20 Posted December 20, 2006 Author Share Posted December 20, 2006 looks like Orio's done it then!Cheers Orio! Link to comment https://forums.phpfreaks.com/topic/31359-solved-sending-variables-across-pages/#findComment-145128 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.