Jump to content

[SOLVED] Sending variables across pages


chriso20

Recommended Posts

Hey there everyone!

Just getting into the magical world of PHP! and by christ is it magical :D

I'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

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]<?php
if(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.
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?
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]<?php

if(isset($_SESSION['error']))
{
echo $_SESSION['error'];
unset($_SESSION['error'];
}

?>[/code]

Orio.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.